Merge remote-tracking branch 'LCTT/master'

This commit is contained in:
Xingyu Wang 2020-06-19 22:56:04 +08:00
commit 3cbd8676a3
6 changed files with 376 additions and 187 deletions

View File

@ -1,8 +1,8 @@
[#]: collector: (lujun9972)
[#]: translator: (wxy)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: reviewer: (wxy)
[#]: publisher: (wxy)
[#]: url: (https://linux.cn/article-12329-1.html)
[#]: subject: (Add interactivity to your Python plots with Bokeh)
[#]: via: (https://opensource.com/article/20/5/bokeh-python)
[#]: author: (Shaun Taylor-Morgan https://opensource.com/users/shaun-taylor-morgan)
@ -10,13 +10,13 @@
使用 Bokeh 为你的 Python 绘图添加交互性
======
> Bokeh 中绘图比其他一些绘图库要复杂一些,但付出额外的努力是有回报的。
> Bokeh 中绘图比其他一些绘图库要复杂一些,但付出额外的努力是有回报的。
![Hands on a keyboard with a Python book ][1]
![](https://img.linux.net.cn/data/attachment/album/202006/18/164708zz7tjxz7m7ax5lt3.jpg)
在这一系列文章中,我通过在每个 Python 绘图库中制作相同的多条形绘图,来了解不同 Python 绘图库的特点。这次我重点介绍的是 [Bokeh][2]读作“BOE-kay”
在这一系列文章中,我通过在每个 Python 绘图库中制作相同的多条形绘图,来研究不同 Python 绘图库的特性。这次我重点介绍的是 [Bokeh][2](读作 “BOE-kay”
Bokeh 中的绘图比[其它一些绘图库][3]要复杂一些,但额外努力是有回报的。Bokeh 的设计既允许你在网络上创建自己的交互式绘图,又能让你详细控制交互性如何工作。我将通过给我在这个系列中使用的多条形图添加一个工具提示来展示这一点。它绘制了 1966 年到 2020 年之间英国选举结果的数据。
Bokeh 中的绘图比[其它一些绘图库][3]要复杂一些,但付出的额外努力是有回报的。Bokeh 的设计既允许你在 Web 上创建自己的交互式绘图,又能让你详细控制交互性如何工作。我将通过给我在这个系列中一直使用的多条形图添加工具提示来展示这一点。它绘制了 1966 年到 2020 年之间英国选举结果的数据。
![][4]
@ -26,10 +26,10 @@ Bokeh 中的绘图比[其它一些绘图库][3]要复杂一些,但额外的努
在我们继续之前,请注意你可能需要调整你的 Python 环境来让这段代码运行,包括以下:
- 运行最新版本的 Python [Linux][11]、[Mac][12] 和 [Windows][13] 的说明)
- 运行最新版本的 Python [Linux][11]、[Mac][12] 和 [Windows][13] 的说明)
- 确认你运行的 Python 版本能与这些库一起工作。
网上有数据,可以用 pandas 导入。
数据可在线获得,可以用 Pandas 导入。
```
import pandas as pd
@ -62,23 +62,20 @@ df = pd.read_csv('https://anvil.works/blog/img/plotting-in-python/uk-election-re
你可以把数据看成是每一个可能的 `(year, party)` 组合的一系列 `seats` 值。这正是 Bokeh 处理的方式。你需要做一个 `(year, party)` 元组的列表:
```
# Get a tuple for each possible (year, party) combination
# 得到每种可能的 (year, party) 组合的元组
x = [(str(r[1]['year']), r[1]['party']) for r in df.iterrows()]
# This comes out as [('1922', 'Conservative'), ('1923', 'Conservative'), ... ('2019', 'Others')]
```
这些将是 x 值。y 值是简单的席位。
这些将是 `x` 值。`y` 值就是席位(`seats`)。
```
y = df['seats']
```
现在你的数据看起来像这样:
现在你的数据看起来应该像这样:
```
x                               y
@ -95,8 +92,7 @@ x                               y
('2019', 'Others')              72
```
Bokeh 需要你将数据封装在它提供的一些对象中,这样它就能给你提供交互功能。将你的 x 和 y 数据结构封装在一个 `ColumnDataSource` 对象中。
Bokeh 需要你将数据封装在它提供的一些对象中,这样它就能给你提供交互功能。将你的 `x``y` 数据结构封装在一个 `ColumnDataSource` 对象中。
```
from bokeh.models import ColumnDataSource
@ -104,8 +100,7 @@ Bokeh 需要你将数据封装在它提供的一些对象中,这样它就能
source = ColumnDataSource(data={'x': x, 'y': y})
```
然后构造一个 `Figure` 对象,并传入你的用 `FactorRange` 对象封装的 x 数据。
然后构造一个 `Figure` 对象,并传入你用 `FactorRange` 对象封装的 `x` 数据。
```
    from bokeh.plotting import figure
@ -114,7 +109,7 @@ Bokeh 需要你将数据封装在它提供的一些对象中,这样它就能
    p = figure(x_range=FactorRange(*x), width=2000, title="Election results")
```
你需要让 Bokeh 创建一个颜色表--这是一个特殊的 `DataSpec` 字典,它根据你给它的颜色映射生成。在这种情况下,颜色表是一个简单的党派名称和一个十六进制值之间的映射。
你需要让 Bokeh 创建一个颜色表这是一个特殊的 `DataSpec` 字典,它根据你给它的颜色映射生成。在这种情况下,颜色表是一个简单的党派名称和一个十六进制值之间的映射。
```
    from bokeh.transform import factor_cmap
@ -130,16 +125,14 @@ Bokeh 需要你将数据封装在它提供的一些对象中,这样它就能
现在你可以创建条形图了:
```
    p.vbar(x='x', top='y', width=0.9, source=source, fill_color=fill_color, line_color=fill_color)
```
Bokeh 图表上数据的可视化表示被称为“<ruby>字形<rt>glyphs</rt></ruby>”,因此你已经创建了一组条形字形。
Bokeh 图表上数据的可视化形式被称为“<ruby>字形<rt>glyphs</rt></ruby>”,因此你已经创建了一组条形字形。
调整图表的细节,让它看起来像你想要的样子。
```
    p.y_range.start = 0
    p.x_range.range_padding = 0.1
@ -150,7 +143,6 @@ Bokeh 图表上数据的可视化表示被称为“<ruby>字形<rt>glyphs</rt></
最后,告诉 Bokeh 你现在想看你的绘图:
```
   from bokeh.io import show
@ -163,18 +155,17 @@ Bokeh 图表上数据的可视化表示被称为“<ruby>字形<rt>glyphs</rt></
*Bokeh 中的多条形绘图©2019年[Anvil][5]*
这已经有了一些互动功能,比如方框缩放。
它已经有了一些互动功能,比如盒子缩放。
![][7] 。
*Bokeh 内置的方框缩放©2019[Anvil][5]*
*Bokeh 内置的盒子缩放©2019[Anvil][5]*
但 Bokeh 的厉害之处在于你可以添加自己的交互性。在下一节中,我们通过在条形图中添加工具提示来探索这个问题。
### 给条形图添加工具提示
要在条形图上添加工具提示,你只需要创建一个 `HoverTool` 对象并将其添加到你的图中。
要在条形图上添加工具提示,你只需要创建一个 `HoverTool` 对象并将其添加到你的绘图中。
```
    h = HoverTool(tooltips=[
@ -192,20 +183,15 @@ Bokeh 图表上数据的可视化表示被称为“<ruby>字形<rt>glyphs</rt></
*选举图,现在带有工具提示(© 2019 [Anvil][5]*
多亏了 Bokeh 的 HTML 输出,当你将绘图嵌入到 Web 应用中时,你可以获得完整的交互体验。你可以把这个例子复制为一个 Anvil 应用[这里][9]Anvil 需要注册才能使用)。
借助 Bokeh 的 HTML 输出,将绘图嵌入到 Web 应用中时,你可以获得完整的交互体验。你可以在[这里][9]把这个例子复制为 Anvil 应用Anvil 需要注册才能使用)。
现在,你可以看到在 Bokeh 中用 `ColumnDataSource` 等对象包装所有数据而付出额外努力的原因了。作为回报,你可以相对轻松地添加交互性。
现在,你可以看到付出额外努力在 Bokeh 中将所有数据封装在 `ColumnDataSource` 等对象的原因了。作为回报,你可以相对轻松地添加交互性。
### 回归简单Altair
Bokeh 是四大最流行的绘图库之一,本系列将研究[它们各自的特别之处][3]。
我也在研究几个因其有趣的方法而脱颖而出的库。接下来,我将看看 [Altair][10],它的声明式 API 意味着它可以做出非常复杂的情节,而不会让你头疼。
* * *
*本文根据 Anvil 博客上的[如何使用 Bokeh 制作绘图][5]改编,经允许后转载。*
我也在研究几个因其有趣的方法而脱颖而出的库。接下来,我将看看 [Altair][10],它的声明式 API 意味着它可以做出非常复杂的绘图,而不会让你头疼。
--------------------------------------------------------------------------------
@ -214,7 +200,7 @@ via: https://opensource.com/article/20/5/bokeh-python
作者:[Shaun Taylor-Morgan][a]
选题:[lujun9972][b]
译者:[wxy](https://github.com/wxy)
校对:[校对者ID](https://github.com/校对者ID)
校对:[wxy](https://github.com/wxy)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
@ -222,7 +208,7 @@ via: https://opensource.com/article/20/5/bokeh-python
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/python-programming-code-keyboard.png?itok=fxiSpmnd (Hands on a keyboard with a Python book )
[2]: https://bokeh.org/
[3]: https://opensource.com/article/20/4/plot-data-python
[3]: https://linux.cn/article-12327-1.html
[4]: https://opensource.com/sites/default/files/uploads/bokeh-closeup.png (A zoomed-in view on the plot)
[5]: https://anvil.works/blog/plotting-in-bokeh
[6]: https://opensource.com/sites/default/files/uploads/bokeh_0.png (A multi-bar plot in Bokeh)

View File

@ -1,5 +1,5 @@
[#]: collector: (lujun9972)
[#]: translator: ( )
[#]: translator: (geekpi)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )

View File

@ -1,5 +1,5 @@
[#]: collector: (lujun9972)
[#]: translator: ( )
[#]: translator: (geekpi)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )

View File

@ -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 dont 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 doesnt ever fail and the loop just keeps running.
```
$ while :
> do
> echo Keep running
> sleep 3
> done
Keep running
Keep running
^C
```
If youve 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 doesnt 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, youre not ever going to want to loop forever, but running until its 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

View File

@ -0,0 +1,203 @@
[#]: collector: (lujun9972)
[#]: translator: ( )
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (Get Your Work Done Faster With These To-Do List Apps on Linux Desktop)
[#]: via: (https://itsfoss.com/to-do-list-apps-linux/)
[#]: author: (Ankush Das https://itsfoss.com/author/ankush/)
Get Your Work Done Faster With These To-Do List Apps on Linux Desktop
======
Getting work done is super important. If you have a planned list of things to do, it makes your work easier. So, its no surprise why were talking about to-do list apps on Linux here.
Sure, you can easily utilize some of the [best note taking apps on Linux][1] for this purpose but using a dedicated to-do app helps you stay focused on work.
You might be aware of some online services for that— but how about some [cool Linux apps][2] that you can use to create a to-do list? In this article, Im going to highlight the best to-do list apps available for Linux.
### Best To-Do List Applications For Desktop Linux Users
![][3]
I have tested these apps on Pop!_OS. I have also tried to mention the installation steps for the mentioned apps but you should check your distributions package manager for details.
**Note:** The list is in no particular order of ranking
#### 1\. Planner
![][4]
Planner is probably the best to-do list app Ive across for Linux distributions.
The best thing is — it is a free and open-source project. It provides a beautiful user interface that aims to give you a meaningful user experience. In other words, its simple and yet attractive.
Not to forget, you get a gorgeous dark mode. As you can see in the screenshot above, you can also choose to add emojis to add some fun to your serious work tasks.
Overall, it looks clean while offering features like the ability to add repeating tasks, creating separate folder/projects, sync with [todoist][5] etc.
#### How to install it?
If youre using [elementary OS][6], you can find it listed in the app center. In either case, they also offer a [Flatpak package on Flathub][7].
Unless you have Flatpak integration in your software center, you should follow our guide to [use Flatpak on Linux][8] to get it installed.
In case you want to explore the source code, take a look at its [GitHub page][9].
[Planner][10]
### 2\. Go For It!
![][11]
Yet another impressive open-source to-do app for Linux which is based on [todotxt][12]. Even though it isnt available for Ubuntu 20.04 (or later) at the time of writing this, you can still use it on machines with Ubuntu 19.10 or older.
In addition to the ability to adding tasks, you can also specify the duration/interval of your break. So, with this to-do app, you will not just end up completing the tasks but also being productive without stressing out.
The user interface is plain and simple with no fancy features. We also have a separate article on [Go][13] [For It][13] — if youd like to know more about it.
You can also use it on your Android phone using the [Simpletask Dropbox app][14].
#### How to install it?
You can type the commands below to install it on any Ubuntu-based distro (prior to Ubuntu 20.04):
```
sudo add-apt-repository ppa:go-for-it-team/go-for-it-stable
sudo apt update
sudo apt install go-for-it
```
In case you want to install it on any other Linux distro, you can try the [Flatpak package on Flathub][15].
If you dont know about Flatpak — take a look at our [complete guide on using Flatpak][8]. To explore more about it, you can also head to their [GitHub page][16].
[Go For It!][16]
#### 3\. GNOME To Do
![][17]
If youre [using Ubuntu][18] or other Linux distribution with GNOME desktop envioenment, you should already have it installed. Just search for “To Do” and you should find it.
Its a simple to-do app which presents the list in the form of cards and you can have separate set of tasks every card. You can add a schedule to the tasks as well. It supports extensions with which you can enable the support for todo.txt files and also integration with [todoist][5].
[GNOME To Do][19]
#### 4\. Taskwarrior [Terminal-based]
![][20]
A command-line based open-source to-do list program “[Taskwarrior][21]” is an impressive tool if you dont need a Graphical User Interface (GUI). It also provides cross-platform support (Windows and macOS).
Its quite easy to add and list tasks along with a due date as shown in the screenshot above.
To make the most out of it, I would suggest you to follow the [official documentation][22] to know how to use it and the options/features that it offers.
##### How to install it?
You can find it in your respective package managers on any Linux distribution. To get it intalled in Ubuntu, you will have to type the following in the terminal:
```
sudo apt install taskwarrior
```
For Manjaro Linux, you can simply get it installed through [pamac][23] that you usually need to [install software in Manjaro Linux.][24]
In case of any other Linux distributions, you should head to its [official download page][25] and follow the instructions.
[Taskwarrior][21]
#### 5\. Task Coach
![][26]
Task Coach is yet another open-source to-do list app that offers quite a lot of essential features. You can add sub-tasks, description to your task, add dates, notes, and a lot more things. It also supports tree view for the task lists you add and manage.
Its a good thing to see that it offers cross-platform support (Windows, macOS, and Android).
Overall, its easy to use with tons of options and works well.
#### How to install it?
It offers both **.deb** and **.rpm** packages for Ubuntu and Fedora. In addition to that, you can also install it using PPA.
You can find all the necessary files and instructions from its [official download page][27].
You may notice an installation error for its dependencies on Ubuntu 20.04. But, I believe it should work fine on the previous Ubuntu releases.
In my case, it worked out fine for me when using the [AUR package][28] through Pamac on Manjaro Linux.
[Task Coach][29]
#### 6\. Todour
![][30]
A very simple open-source to-do list app that lets you utilize todo.txt file as well. You may not get a lot of options to choose from — but you get a couple of useful settings to tweak.
It may not be the most actively developed to-do list app — but it does the work expected.
#### How to install Todour?
If youre using Manjaro Linux, you can utilize pamac to install Todour from [AUR][28].
Unfortunately, it does not provide any **.deb** or **.rpm** package for Ubuntu/Fedora. So, youll have to build it from source or just explore more about it on its [GitHub page][31].
[Todour][32]
### Wrapping Up
As an interesting mention, Id like you to take a look at [TodoList][33], which is an applet for KDE-powered distributions. Among mainstream to-do list applications, [Remember The Milk is the rare one that provides a Linux client][34]. It is not open source, though.
I hope this list of to-do specific apps help you get things done on Linux.
Did I miss any of your favorite to-do list apps on Linux? Feel free to let me know what you think!
--------------------------------------------------------------------------------
via: https://itsfoss.com/to-do-list-apps-linux/
作者:[Ankush Das][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://itsfoss.com/author/ankush/
[b]: https://github.com/lujun9972
[1]: https://itsfoss.com/note-taking-apps-linux/
[2]: https://itsfoss.com/essential-linux-applications/
[3]: https://i1.wp.com/itsfoss.com/wp-content/uploads/2020/06/open-Source-to-do-list-apps.jpg?ssl=1
[4]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2020/05/planner-screenshot.jpg?ssl=1
[5]: https://todoist.com
[6]: https://elementary.io
[7]: https://flathub.org/apps/details/com.github.alainm23.planner
[8]: https://itsfoss.com/flatpak-guide/
[9]: https://github.com/alainm23/planner
[10]: https://planner-todo.web.app/
[11]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2020/01/go-for-it-reminders.jpg?ssl=1
[12]: http://todotxt.com
[13]: https://itsfoss.com/go-for-it-to-do-app-in-linux/
[14]: https://play.google.com/store/apps/details?id=nl.mpcjanssen.todotxtholo&hl=en
[15]: https://flathub.org/apps/details/de.manuel_kehl.go-for-it
[16]: https://github.com/JMoerman/Go-For-It
[17]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2020/05/to-do-gnome.jpg?ssl=1
[18]: https://itsfoss.com/getting-started-with-ubuntu/
[19]: https://wiki.gnome.org/Apps/Todo/Download
[20]: https://i1.wp.com/itsfoss.com/wp-content/uploads/2020/06/taskwarrior.png?ssl=1
[21]: https://taskwarrior.org/
[22]: https://taskwarrior.org/docs/start.html
[23]: https://wiki.manjaro.org/index.php?title=Pamac
[24]: https://itsfoss.com/install-remove-software-manjaro/
[25]: https://taskwarrior.org/download/
[26]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2020/06/task-coach.png?ssl=1
[27]: https://www.taskcoach.org/download.html
[28]: https://itsfoss.com/aur-arch-linux/
[29]: https://www.taskcoach.org/index.html
[30]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2020/06/todour.png?ssl=1
[31]: https://github.com/SverrirValgeirsson/Todour
[32]: https://nerdur.com/todour-pl/
[33]: https://store.kde.org/p/1152230/
[34]: https://itsfoss.com/remember-the-milk-linux/

View File

@ -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