提问人:Brian Deacon 提问时间:8/18/2009 最后编辑:oguz ismailBrian Deacon 更新时间:5/1/2023 访问量:994509
Bash 中单行 while 循环的语法
Syntax for a single-line while loop in Bash
答:
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
评论
cmdhist
while true; do sh /Users/myuser/bin/vpn ; 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
评论
&&
&&
;
sleep
sleep 1h
h
man sleep
sleep
如果我能举两个实际的例子(带有一点“情感”)。
这将写入文件夹“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
只是想做出贡献。
评论
ls -1
*.jpg 和 rm *.jpg
也可以在 while 的条件下使用睡眠命令。恕我直言,让单行看起来更干净。
while sleep 2; do echo thinking; done
评论
while echo thinking ; do sleep 2 ; done
while echo thinking || true ; do sleep 2 ; done
while true ; ...
冒号总是“真的”:
while :; do foo; sleep 2; done
评论
: [arguments]
true
dash
bash
true
true``* is widely used in historical scripts and *is less cryptic to novice script readers*.
您还可以使用命令: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
评论
until ((0)); do foo; sleep 2; done
一个非常简单的无限循环。:)
while true ; do continue ; done
你的问题是:
while true; do foo ; sleep 2 ; done
对于简单的过程监视,请改用watch
你也可以试试这个 警告:你不应该这样做,但由于问题是要求无限循环,没有尽头......这就是你如何做到的。
while [[ 0 -ne 1 ]]; do echo "it's looping"; sleep 2; done
评论
如果您希望 while 循环在某个条件后停止,并且您的命令在满足此条件时返回非零,那么您可以像这样让循环中断:foo
while foo; do echo 'sleeping...'; sleep 5; done;
例如,如果命令正在批量删除内容,并且在没有要删除的内容时返回 1。foo
如果您有一个自定义脚本,该脚本需要多次运行命令,直到出现某种情况,则这很有效。编写脚本以在满足条件时退出,并在应再次运行时退出。1
0
例如,假设您有一个 python 脚本,该脚本更新数据库中的 100 行,如果有更多要更新的行,如果没有更多行,则返回。以下命令将允许您一次更新第 100 行,并在更新之间休眠 5 秒:batch_update.py
0
1
while batch_update.py; do echo 'sleeping...'; sleep 5; done;
评论
foo
1
用:while
while true; do echo 'while'; sleep 2s; done
使用循环:for
for ((;;)); do echo 'forloop'; sleep 2; done
使用 ,(与上面略有不同,键盘中断不会停止它)Recursion
list(){ echo 'recursion'; sleep 2; list; } && list;
评论
sleep
sleep 1h
h
man sleep
sleep
您甚至不需要使用 和 .对于无限循环,我发现使用大括号更具可读性。例如:do
done
for
for ((;;)) { date ; sleep 1 ; }
这在 和 中有效。在 中不起作用。bash
zsh
sh
您也可以将该循环置于后台(例如,当您需要断开与远程计算机的连接时)
nohup bash -c "while true; do aws s3 sync xml s3://bucket-name/xml --profile=s3-profile-name; sleep 3600; done &"
评论
在(甚至更早)中,您还可以通过运行循环条件中的所有内容来反转 的作用,但改用循环正文:bash 5
:
:
while ( gdate +"%c ( %s.%-N )" && sleep 0.71 ) do :; done # discards side-effects
while { gdate +"%c ( %s.%-N )" && sleep 0.71; } do :; done # no sub-shell needed
+ gdate '+%c ( %s.%-N )'
Sun Apr 30 12:38:25 2023 ( 1682872705.498728 )
+ sleep 0.71
+ :
+ gdate '+%c ( %s.%-N )'
Sun Apr 30 12:38:26 2023 ( 1682872706.218152 )
+ sleep 0.71
...
同时更宽容,即使没有zsh
:
while; do gdate +'%c ( %s.%-N )' && sleep 0.31 ; done
Sun Apr 30 12:46:09 2023 ( 1682873169.469092 )
Sun Apr 30 12:46:09 2023 ( 1682873169.789560 )
Sun Apr 30 12:46:10 2023 ( 1682873170.105635 )
Sun Apr 30 12:46:10 2023 ( 1682873170.424766 )
上一个:循环反向真的更快吗?
评论