2017-02-14 4 views
0

しばらくしてから、シェルスクリプトからMySQLクエリを実行したかったのです。私はスクリプトを書いたが、出力は期待通りではない。同じ値を再度データベースから出力しています。誰かが助けてくれますか?ある時間間隔の後にシェルスクリプトからMySQLクエリを呼び出す

$ ./demo.sh 
Shell Script to Query a Database! 
--Querying from the Database starts-- 
mysql: [Warning] Using a password on the command line interface can be insecure. 
id 
282 
Wait for 2 seconds for the next ID. 
mysql: [Warning] Using a password on the command line interface can be insecure. 
id 
282 
Wait for 2 seconds for the next ID. 
mysql: [Warning] Using a password on the command line interface can be insecure. 
id 
282 
Wait for 2 seconds for the next ID. 
mysql: [Warning] Using a password on the command line interface can be insecure. 
id 
282 
Wait for 2 seconds for the next ID. 
mysql: [Warning] Using a password on the command line interface can be insecure. 
id 
282 
Wait for 2 seconds for the next ID. 
--Query Stopped!-- 

SCRIPT- demo.sh

#!/bin/bash 
echo "Shell Script to Query a Database!" 
echo "--Querying from the Database starts--" 
cd "C:\Program Files\MySQL\MySQL Server 5.7\bin" 
for i in {1..5}; 
do 
./mysql -u root [email protected] << EOF 
use catalyst_latest; 
select id from ci_master limit 1; 
EOF 
echo "Wait for 2 seconds for the next ID." 
sleep 2; 
done 
echo "--Query Stopped!--" 

OUTPUTあなたは282が毎回返さなっていることがわかりますか?だから私は2秒後にデータベースから次のIDを欲しい。それをどうやるか教えてください。 ありがとうございます。

答えて

0

あなたはbashの変数に返さid値を覚えておく必要がありますし、このようなクエリのWHERE句でその変数を再利用:

select id from ci_master where id > $id limit 1 

変数の使用には、このような何かをコマンドの出力を書き込むには:

id=$(mysql <options> -Nsqe "<query>") 

(IDなど)カラム名をスキップするオプションN、よりサイレント出力するためのオプションs、結果をキャッシュしないためのオプションq、オプション01コマンドを実行するにはを入力します。

最初に$id変数は、1またはテーブル内の既存の値の最小値のような値に設定する必要があります。

+0

@アレクサンダーありがとうございます。私はこのようなことをしました - –

0
#!/bin/bash 

echo "Shell Script to Query a Database!" 
echo "--Querying from the Database starts--" 
cd "C:\Program Files\MySQL\MySQL Server 5.7\bin" 
for i in {1..10}; 
do 
temp=`./mysql -u root [email protected] << EOF 
use catalyst_latest; 
select id from ci_master limit $i; 
EOF` 
result='' 
for j in $temp 
do 
result=$j 
done 
#date; 
echo "$result" 
echo "Wait for 1 seconds for the next ID." 
sleep 1; 
done 
echo "--Query Stopped!--" 
関連する問題