这是 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)
条件停止:
如果您仅取消注释此行:
if [[ ! $str =~ 5 ]]; then
Run Code Online (Sandbox Code Playgroud)
它会5在输出中存在时循环(例如 while from 50until 00)
如果您仅取消注释此行:
if [[ $str =~ 7 ]]; then
Run Code Online (Sandbox Code Playgroud)
它将循环直到7存在于输出中(即直到当前秒数 = 07、17、27、37、47 或 57)
不包含字符串的示例输出(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)
| 归档时间: |
|
| 查看次数: |
7408 次 |
| 最近记录: |