我很难找到分号和/或大括号的正确组合。我想这样做,但作为命令行的一行代码:
while [ 1 ]
do
foo
sleep 2
done
我很难找到分号和/或大括号的正确组合。我想这样做,但作为命令行的一行代码:
while [ 1 ]
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 true; do foo; sleep 2; done
顺便说一句,如果在命令提示符下键入多行(如图所示),然后用箭头向上调用历史记录,则会在一行中得到正确的标点符号。
$ while true
> do
> echo "hello"
> sleep 2
> done
hello
hello
hello
^C
$ <arrow up> while true; do echo "hello"; sleep 2; done
可以使用分号分隔语句:
$ while [ 1 ]; 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
您还可以将该循环置于后台(例如,当您需要断开与远程计算机的连接时)
nohup bash -c "while true; do aws s3 sync xml s3://bucket-name/xml --profile=s3-profile-name; sleep 3600; done &"
如果我能举两个实际的例子(带点“情绪”)。
这将写入文件夹“img”中所有以“.jpg”结尾的文件的名称:
for f in *; do if [ "${f#*.}" == 'jpg' ]; then echo $f; fi; done
这将删除它们:
for f in *; do if [ "${f#*.}" == 'jpg' ]; then rm -r $f; fi; done
只是想做出贡献。