循环 bash 命令,直到输出不再包含字符串

mak*_*ake 3 bash

如何循环bash命令直到输出不再包含字符串,然后打印循环停止输出的时间?watch命令不可用。

icy*_*com 9

这是 running 的示例date +%S,它每半秒打印当前时间的秒部分,并在某个条件下停止(见下文):

while true; do
  str=`date +%S`
  echo Output: $str
  # Use the below when you want the output not to contain some string
  if [[ ! $str =~ 5 ]]; then
  # Use the below when you want the output to contain some string
  # if [[ $str =~ 7 ]]; then
    break
  fi
  sleep .5
done
echo Finished: `date`
Run Code Online (Sandbox Code Playgroud)

条件停止:

不包含字符串的示例输出(5在本例中):

Output: 56
Output: 57
Output: 57
Output: 58
Output: 58
Output: 59
Output: 59
Output: 00
Finished: Thu Mar 1 20:16:00 EST 2012
Run Code Online (Sandbox Code Playgroud)

包含字符串的示例输出(7在本例中):

Output: 08
Output: 09
Output: 09
Output: 10
Output: 10
Output: 11
Output: 11
Output: 12
Output: 12
Output: 13
Output: 13
Output: 14
Output: 14
Output: 15
Output: 15
Output: 16
Output: 16
Output: 17
Finished: Thu Mar 1 19:58:17 EST 2012
Run Code Online (Sandbox Code Playgroud)