bash 使用脚本在 shell 中创建命令循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3221081/
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-17 22:18:50  来源:igfitidea点击:

Making a command loop in shell with a script

bashshellloops

提问by cdated

How can one loop a command/program in a Unix shell without writing the loop into a script or other application.

如何在 Unix shell 中循环命令/程序而不将循环写入脚本或其他应用程序。

For example, I wrote a script that outputs a light sensor value but I'm still testing it right now so I want it run it in a loop by running the executable repeatedly.

例如,我编写了一个输出光传感器值的脚本,但我现在仍在测试它,所以我希望它通过重复运行可执行文件来循环运行它。

Maybe I'd also like to just run "ls" or "df" in a loop. I know I can do this easily in a few lines of bash code, but being able to type a command in the terminal for any given set of command would be just as useful to me.

也许我还想循环运行“ls”或“df”。我知道我可以在几行 bash 代码中轻松完成此操作,但是能够在终端中为任何给定的命令集键入命令对我来说同样有用。

回答by Federico klez Culloca

You can write the exact same loop you would write in a shell script by writing it in one line putting semicolons instead of returns, like in

您可以编写与在 shell 脚本中编写的完全相同的循环,方法是将其写在一行中,并放置分号而不是返回,例如

for NAME [in LIST ]; do COMMANDS; done

At that point you could write a shell script called, for example, repeatthat, given a command, runs it N times, by simpling changing COMMANDSwith $1.

在这一点上,您可以编写一个名为的 shell 脚本,例如repeat,通过简单地更改COMMANDS$1.

回答by Eric

This doesn't exactly answer your question, but I felt it was relavent. One of the great things with shell looping is that some commands return lists of items. Of course that is obvious, but a something you can do using the for loop is execute a command on that list of items.

这并不能完全回答你的问题,但我觉得它是相关的。shell 循环的一大优点是某些命令返回项目列表。当然,这是显而易见的,但是您可以使用 for 循环执行的操作是在该项目列表上执行命令。

for $file in `find . -name *.wma`; do cp $file  ./new/location/ done;

You can get creative and do some very powerful stuff.

你可以发挥创造力并做一些非常强大的事情。

回答by Spidey

I recommend the use of "watch", it just do exactly what you want, and it cleans the terminal before each execution of the commands, so it's easy to monitor changes.

我推荐使用“watch”,它只是做你想做的事,并且在每次执行命令之前清理终端,所以很容易监控变化。

You probably have it already, just try watch lsor watch ./my_script.sh. You can even control how much time to wait between each execution, in seconds, with the -n option, and you can use -d to highlight the difference in the output of consecutive runs.

您可能已经拥有它,只需尝试watch lswatch ./my_script.sh。您甚至可以使用 -n 选项控制每次执行之间的等待时间(以秒为单位),并且您可以使用 -d 突出显示连续运行输出的差异。

Try:

尝试:

Run ls each second:

每秒运行一次:

watch -n 1 ls

Run my_script.sh each 3 seconds, and highlight differences:

每 3 秒运行一次 my_script.sh,并突出显示差异:

watch -n 3 -d ./my_script.sh

watch program man page: http://linux.die.net/man/1/watch

观看程序手册页:http: //linux.die.net/man/1/watch

回答by Benson

Aside from accepting arguments, anything you can do in a script can be done on the command line. Earlier I typed this directly in to bash to watch a directory fill up as I transferred files:

除了接受参数之外,您可以在脚本中执行的任何操作都可以在命令行上完成。早些时候,我将这个直接输入到 bash 中,以便在我传输文件时观察目录是否填满:

while sleep 5s
do
  ls photos
end