我很难找到分号和/或大括号的正确组合。我想这样做,但作为命令行的一行代码:
while [ 1 ]
do
foo
sleep 2
done
我很难找到分号和/或大括号的正确组合。我想这样做,但作为命令行的一行代码:
while [ 1 ]
do
foo
sleep 2
done
当前回答
可以使用分号分隔语句:
$ while [ 1 ]; do foo; sleep 2; done
其他回答
对于简单的过程监视,请改用监视
您还可以使用until命令:
until ((0)); do foo; sleep 2; done
注意,与while相反,只要测试条件具有不为零的退出状态,until就会在循环内执行命令。
使用while循环:
while read i; do foo; sleep 2; done < /dev/urandom
使用for循环:
for ((;;)); do foo; sleep 2; done
使用until的另一种方法:
until [ ]; do foo; sleep 2; done
我只喜欢在WHILE语句中使用分号,和&&运算符使循环做多件事。。。
所以我总是这样做
while true ; do echo Launching Spaceship into orbit && sleep 5s && /usr/bin/launch-mechanism && echo Launching in T-5 && sleep 1s && echo T-4 && sleep 1s && echo T-3 && sleep 1s && echo T-2 && sleep 1s && echo T-1 && sleep 1s && echo liftoff ; done
你也可以试试这个警告:您不应该这样做,但因为问题是要求无限循环没有结束。。。这就是你能做到的。
while [[ 0 -ne 1 ]]; do echo "it's looping"; sleep 2; done
您还可以将该循环置于后台(例如,当您需要断开与远程计算机的连接时)
nohup bash -c "while true; do aws s3 sync xml s3://bucket-name/xml --profile=s3-profile-name; sleep 3600; done &"