
出力にエラーを示す特定の文字列がある間、コマンドを繰り返したいと思います。 コマンドは です。に がgksu ./installer.run > ./inst.log 2>&1
ある間、これを繰り返したいのです。 これを bash コマンドラインから実行するにはどうすればよいですか?'string'
./inst.log
答え1
ファイル内の文字列を検索:
grep -q string file
終了値は、grep が何かを見つけたかどうかを示します。
次に、コマンドが true の終了値を返す限りループすることができます。
while command ; do
repeat this
done
ただし、コマンドを少なくとも1回は実行したいので、
while true ; do
some command
if ! grep -q string file ; then
break # jump out of the loop if it's _not_ there
fi
done
そうでない場合は、ループの前とループ内でコマンドを繰り返す必要があります。
答え2
ではwhile while-cmd-list; do do-cmd-list; done
、while-cmd-list
またコマンドのリスト。単一のコマンドである必要はありません。
つまり、次のことができます:
while
gksu ./installer.run > ./inst.log 2>&1
grep -q string inst.log
do
echo >&2 "Trying again, output contained string"
done
ただし、ここでは次のことも実行できます。
while
gksu ./installer.run 2>&1 |
tee ./inst.log |
grep string > /dev/null
do
echo >&2 "Trying again, output contained string"
done
( は使用していないことに注意してください。使用する-q
と、grep
早期に終了してインストーラーが SIGPIPE を受信する可能性があります)。