diff --git a/sources/tech/20200615 How to use Bash history commands.md b/sources/tech/20200615 How to use Bash history commands.md index 64f76a84d2..796cb1b8ba 100644 --- a/sources/tech/20200615 How to use Bash history commands.md +++ b/sources/tech/20200615 How to use Bash history commands.md @@ -1,5 +1,5 @@ [#]: collector: (lujun9972) -[#]: translator: ( ) +[#]: translator: (geekpi) [#]: reviewer: ( ) [#]: publisher: ( ) [#]: url: ( ) diff --git a/sources/tech/20200616 How to loop forever in bash on Linux.md b/sources/tech/20200616 How to loop forever in bash on Linux.md deleted file mode 100644 index 5b6b588172..0000000000 --- a/sources/tech/20200616 How to loop forever in bash on Linux.md +++ /dev/null @@ -1,147 +0,0 @@ -[#]: collector: (lujun9972) -[#]: translator: (geekpi) -[#]: reviewer: ( ) -[#]: publisher: ( ) -[#]: url: ( ) -[#]: subject: (How to loop forever in bash on Linux) -[#]: via: (https://www.networkworld.com/article/3562576/how-to-loop-forever-in-bash-on-linux.html) -[#]: author: (Sandra Henry-Stocker https://www.networkworld.com/author/Sandra-Henry_Stocker/) - -How to loop forever in bash on Linux -====== - -[tine ivanic][1] [(CC0)][2] - -There are a number of ways to loop forever (or until you decide to stop) on Linux and you can do this on the command line or within scripts. - -The **for** and **while** commands make the job quite easy. There are only a few things to keep in mind with respect to syntax and tactics. - -**[ Also see [Invaluable tips and tricks for troubleshooting Linux][3]. ]** - -### Using while - -One of the easiest forever-loops involves using the **while** command followed by the condition "true". You don’t have to bother with logic like **while [ 1 -eq 1 ]** or similar tests. The **while true** test means the loop will run until you stop it with **CTRL-C**, close the terminal window or log out. Here's an example: - -``` -$ while true -> do -> echo Keep running -> sleep 3 -> done -Keep running -Keep running -Keep running -^C -``` - -You can also do the same thing with **while :**. The key here is that the **:** always yields success so, like **while true**, this test doesn’t ever fail and the loop just keeps running. - -``` -$ while : -> do -> echo Keep running -> sleep 3 -> done -Keep running -Keep running -^C -``` - -If you’ve inserted an infinite loop into a script and want to remind the person who is using it how to exit the script, you can always add a hint using the **echo** command: - -``` -while : -do - echo Keep running - echo "Press CTRL+C to exit" - sleep 1 -done -``` - -### Using for - -The **for** command also provides an easy way to loop forever. While not quite as obvious as **while true**, the syntax is reasonably straightforward. You just replace the parameters in a bounded loop that would generally look something like this "start with c equal to 1 and increment it until reaches 5" specification: - -``` -$ for (( c=1; c<=5; c++ )) -``` - -with one that doesn’t specify any parameters: - -``` -$ for (( ; ; )) -``` - -With no start value, increment or exit test, this loop will run forever or until it is forcibly stopped. - -``` -$ for (( ; ; )) -> do -> echo Keep running -> echo “Press CTRL+C to exit” -> sleep 2 -> done -Keep your spirits up -Keep your spirits up -Keep your spirits up -``` - -### Why loop forever? - -In real life, you’re not ever going to want to loop forever, but running until it’s time to go home, the work is done or you run into a problem is not at all unusual. Any loop that is constructed as an infinite loop can also be set up to be exited depending on various circumstances. - -This script would keep processing data until 5 p.m. or the first time it checks the time after 5 p.m.: - -``` -#!/bin/bash - -while true -do - if [ `date +%H` -ge 17 ]; then - exit # exit script - fi - echo keep running - ~/bin/process_data # do some work -done -``` - -If you want to exit the loop instead of exiting the script, use a **break** command instead of an **exit**. - -``` -#!/bin/bash - -while true -do - if [ `date +%H` -ge 17 ]; then - break # exit loop - fi - echo keep running - ~/bin/process_data -done -… run other commands here … -``` - -#### Wrap-Up - -Looping forever is easy. Specifying the conditions under which you want to stop looping takes a little extra effort. - -Join the Network World communities on [Facebook][4] and [LinkedIn][5] to comment on topics that are top of mind. - --------------------------------------------------------------------------------- - -via: https://www.networkworld.com/article/3562576/how-to-loop-forever-in-bash-on-linux.html - -作者:[Sandra Henry-Stocker][a] -选题:[lujun9972][b] -译者:[译者ID](https://github.com/译者ID) -校对:[校对者ID](https://github.com/校对者ID) - -本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 - -[a]: https://www.networkworld.com/author/Sandra-Henry_Stocker/ -[b]: https://github.com/lujun9972 -[1]: https://unsplash.com/photos/u2d0BPZFXOY -[2]: https://creativecommons.org/publicdomain/zero/1.0/ -[3]: https://www.networkworld.com/article/3242170/linux/invaluable-tips-and-tricks-for-troubleshooting-linux.html -[4]: https://www.facebook.com/NetworkWorld/ -[5]: https://www.linkedin.com/company/network-world diff --git a/translated/tech/20200616 How to loop forever in bash on Linux.md b/translated/tech/20200616 How to loop forever in bash on Linux.md new file mode 100644 index 0000000000..cb71098b4a --- /dev/null +++ b/translated/tech/20200616 How to loop forever in bash on Linux.md @@ -0,0 +1,147 @@ +[#]: collector: (lujun9972) +[#]: translator: (geekpi) +[#]: reviewer: ( ) +[#]: publisher: ( ) +[#]: url: ( ) +[#]: subject: (How to loop forever in bash on Linux) +[#]: via: (https://www.networkworld.com/article/3562576/how-to-loop-forever-in-bash-on-linux.html) +[#]: author: (Sandra Henry-Stocker https://www.networkworld.com/author/Sandra-Henry_Stocker/) + +如何在 Linux 的 bash 中永远循环 +====== + +[tine ivanic][1] [(CC0)][2] + +在 Linux 中有很多永远循环(或直到你决定停止)的方法,你可以在命令行或脚本中执行此操作。 + +**for** 和 **while** 命令使这件事非常容易。 关于语法和策略,只有几件事要牢记。 + +**另请参见[用于排除 Linux 故障的宝贵技巧][3]。** + +### 使用 while + +最简单的永远循环之一是使用 **while** 命令,后面跟上条件 “true”。 你不必担心诸如 **while [ 1 -eq 1 ]** 之类的逻辑或类似的测试。 **while true** 测试表示循环将一直运行,直到你使用 **CTRL-C** 停止循环、关闭终端窗口或注销为止。 这是一个例子: + +``` +$ while true +> do +> echo Keep running +> sleep 3 +> done +Keep running +Keep running +Keep running +^C +``` + +你也可以使用 **while :** 做同样的事情。 这里的关键是 **:** 总是返回成功,因此就像 **while true** 一样,此测试永远不会失败,并且循环会继续运行。 + +``` +$ while : +> do +> echo Keep running +> sleep 3 +> done +Keep running +Keep running +^C +``` + +如果你在脚本中插入了无限循环,并想提醒使用它的人如何退出脚本,那么可以使用 **echo** 命令添加提示: + +``` +while : +do + echo Keep running + echo "Press CTRL+C to exit" + sleep 1 +done +``` + +### 使用 for + +**for** 命令还提供了一种永远循环的简便方法。虽然不如 **while true** 明显,但语法相当简单。你只需要在有界循环中替换参数即可,界限通常类似于 “c 从等于 1 开始递增,直到 5”: + +``` +$ for (( c=1; c<=5; c++ )) +``` + +不指定任何参数的情况下: + +``` +$ for (( ; ; )) +``` + +没有起始值、增量或退出测试,此循环将永远运行或被强制停止。 + +``` +$ for (( ; ; )) +> do +> echo Keep running +> echo “Press CTRL+C to exit” +> sleep 2 +> done +Keep your spirits up +Keep your spirits up +Keep your spirits up +``` + +### Why loop forever? + +在现实中,你不会想永远循环下去,但一直运行直到想要回家、工作完成或者遇到问题才退出并不罕见。任何构造为无限循环的循环都可以设置为根据各种情况退出。 + +该脚本将一直处理数据直到下午 5 点。 或检查发现第一次超过 5 点的时间: + +``` +#!/bin/bash + +while true +do + if [ `date +%H` -ge 17 ]; then + exit # exit script + fi + echo keep running + ~/bin/process_data # do some work +done +``` + +如果要退出循环而不是退出脚本,请使用 **break** 命令而不是 **exit**。 + +``` +#!/bin/bash + +while true +do + if [ `date +%H` -ge 17 ]; then + break # exit loop + fi + echo keep running + ~/bin/process_data +done +… run other commands here … +``` + +#### 总结 + +永远循环很容易。 指定要停止循环的条件需要花费一些额外的精力。 + +加入 [Facebook][4] 和 [LinkedIn][5] 上的 Network World 社区,评论热门主题。 + +-------------------------------------------------------------------------------- + +via: https://www.networkworld.com/article/3562576/how-to-loop-forever-in-bash-on-linux.html + +作者:[Sandra Henry-Stocker][a] +选题:[lujun9972][b] +译者:[geekpi](https://github.com/geekpi) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: https://www.networkworld.com/author/Sandra-Henry_Stocker/ +[b]: https://github.com/lujun9972 +[1]: https://unsplash.com/photos/u2d0BPZFXOY +[2]: https://creativecommons.org/publicdomain/zero/1.0/ +[3]: https://www.networkworld.com/article/3242170/linux/invaluable-tips-and-tricks-for-troubleshooting-linux.html +[4]: https://www.facebook.com/NetworkWorld/ +[5]: https://www.linkedin.com/company/network-world