单行 Bash 无限 while 循环的语法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1289026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 18:25:03  来源:igfitidea点击:

Syntax for a single-line Bash infinite while loop

bashloopswhile-loop

提问by Brian Deacon

I am having trouble coming up with the right combination of semicolons and/or braces. I'd like to do this, but as a one-liner from the command line:

我在想出分号和/或大括号的正确组合时遇到了麻烦。我想这样做,但作为命令行中的单行:

while [ 1 ]
do
    foo
    sleep 2
done

回答by Stefano Borini

while true; do foo; sleep 2; done

By the way, if you type it as a multiline (as you are showing) at the command prompt and then call the history with arrow up, you will get it on a single line, correctly punctuated.

顺便说一句,如果您在命令提示符下将其键入为多行(如您所示),然后使用向上箭头调用历史记录,您将在一行中找到它,标点正确。

$ while true
> do
>    echo "hello"
>    sleep 2
> done
hello
hello
hello
^C
$ <arrow up> while true; do    echo "hello";    sleep 2; done

回答by Anssi

It's also possible to use sleep command in while's condition. Making one-liner looking more clean imho.

也可以在 while 条件下使用 sleep 命令。使单衬看起来更干净恕我直言。

while sleep 2; do echo thinking; done

回答by ajaaskel

Colon is always "true":

冒号总是“真”:

while :; do foo; sleep 2; done

回答by mipadi

You can use semicolons to separate statements:

您可以使用分号分隔语句:

$ while [ 1 ]; do foo; sleep 2; done

回答by devnull

You can also make use of untilcommand:

您还可以使用until命令:

until ((0)); do foo; sleep 2; done

Note that in contrast to while, untilwould execute the commands inside the loop as long as the test condition has an exit status which is not zero.

请注意,与 相比whileuntil只要测试条件的退出状态不为零,就会在循环内执行命令。



Using a whileloop:

使用while循环:

while read i; do foo; sleep 2; done < /dev/urandom


Using a forloop:

使用for循环:

for ((;;)); do foo; sleep 2; done


Another way using until:

另一种使用方式until

until [ ]; do foo; sleep 2; done

回答by Mack

A very simple infinite loop.. :)

一个非常简单的无限循环.. :)

while true ; do continue ; done

Fr your question it would be:

你的问题是:

while true; do foo ; sleep 2 ; done

回答by Thomas

I like to use the semicolons only for the WHILE statement, and the && operator to make the loop do more than one thing...

我喜欢只对 WHILE 语句使用分号,而使用 && 运算符使循环做不止一件事......

So I always do it like this

所以我总是这样做

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

回答by Christian Schwarz

For simple process watching use watchinstead

对于简单的过程观看使用watch代替

回答by Muhammad Soliman

Using while:

使用while

while true; do echo 'while'; sleep 2s; done

Using forLoop:

使用for循环:

for ((;;)); do echo 'forloop'; sleep 2; done

Using Recursion, (a little bit different than above, keyboard interrupt won't stop it)

使用Recursion, (与上面有点不同,键盘中断不会停止它)

list(){ echo 'recursion'; sleep 2; list; } && list;

回答by Yep_It's_Me

If you want the while loop to stop after some condition, and your foocommand returns non-zero when this condition is met then you can get the loop to break like this:

如果您希望 while 循环在某个条件后停止,并且您的foo命令在满足此条件时返回非零值,那么您可以像这样中断循环:

while foo; do echo 'sleeping...'; sleep 5; done;

For example, if the foocommand is deleting things in batches, and it returns 1 when there is nothing left to delete.

例如,如果foo命令是批量删除东西,当没有东西可以删除时返回1。

This works well if you have a custom script that needs to run a command many times until some condition. You write the script to exit with 1when the condition is met and exit with 0when it should be run again.

如果您有一个需要多次运行命令直到出现某种情况的自定义脚本,这很有效。您编写脚本以1在满足条件0时退出,并在应再次运行时退出。

For example, say you have a python script batch_update.pywhich updates 100 rows in a database and returns 0if there are more to update and 1if there are no more. The the following command will allow you to update rows 100 at a time with sleeping for 5 seconds between updates:

例如,假设您有一个 Python 脚本batch_update.py,它更新数据库中的 100 行,0如果有更多行需要更新,1如果没有更多行,则返回。以下命令将允许您一次更新 100 行,更新之间休眠 5 秒:

while batch_update.py; do echo 'sleeping...'; sleep 5; done;