mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-25 23:11:02 +08:00
Merge remote-tracking branch 'LCTT/master'
This commit is contained in:
commit
73690520b7
@ -1,26 +1,26 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (geekpi)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: reviewer: (wxy)
|
||||
[#]: publisher: (wxy)
|
||||
[#]: url: (https://linux.cn/article-11651-1.html)
|
||||
[#]: subject: (A quick introduction to Toolbox on Fedora)
|
||||
[#]: via: (https://fedoramagazine.org/a-quick-introduction-to-toolbox-on-fedora/)
|
||||
[#]: author: (Ryan Walter https://fedoramagazine.org/author/rwaltr/)
|
||||
|
||||
快速介绍 Fedora 中的 Toolbox
|
||||
Fedora 中的 Toolbox 简介
|
||||
======
|
||||
|
||||
![][1]
|
||||
|
||||
Toolbox 使你可以[在容器中分类和管理开发环境][2],而无需 root 权限或手动添加卷。它创建一个容器,你可以在其中安装自己的命令行工具,而无需在基础系统中安装它们。当你没有root 权限或无法直接安装程序时,也可以使用它。本文会介绍 Toolbox 及其功能。
|
||||
Toolbox 使你可以[在容器中分类和管理开发环境][2],而无需 root 权限或手动添加卷。它创建一个容器,你可以在其中安装自己的命令行工具,而无需在基础系统中安装它们。当你没有 root 权限或无法直接安装程序时,也可以使用它。本文会介绍 Toolbox 及其功能。
|
||||
|
||||
### 安装 Toolbox
|
||||
|
||||
[Silverblue][3] 默认包含 Toolbox。对于 Workstation 和 Server 版本,你可以使用 _dnf install toolbox_ 从默认仓库中获取它。
|
||||
[Silverblue][3] 默认包含 Toolbox。对于 Workstation 和 Server 版本,你可以使用 `dnf install toolbox` 从默认仓库中获取它。
|
||||
|
||||
### 创建 Toolbox
|
||||
|
||||
打开终端并运行 _toolbox enter_。程序将自动请求许可来下载最新的镜像,创建第一个容器并将你的 shell 放在该容器中。
|
||||
打开终端并运行 `toolbox enter`。程序将自动请求许可来下载最新的镜像,创建第一个容器并将你的 shell 放在该容器中。
|
||||
|
||||
```
|
||||
$ toolbox enter
|
||||
@ -29,7 +29,7 @@ Image required to create toolbox container.
|
||||
Download registry.fedoraproject.org/f30/fedora-toolbox:30 (500MB)? [y/N]: y
|
||||
```
|
||||
|
||||
当前,toolbox 和你的基本系统之间没有区别。你的文件系统和软件包未更改。这是一个使用仓库的示例,它包含 _~/src/resume_ 文件夹下的简历的文档源。简历是使用 _pandoc_ 工具构建的。
|
||||
当前,Toolbox 和你的基本系统之间没有区别。你的文件系统和软件包未曾改变。下面是一个使用仓库的示例,它包含 `~/src/resume` 文件夹下的简历的文档源文件。简历是使用 `pandoc` 工具构建的。
|
||||
|
||||
```
|
||||
$ pwd
|
||||
@ -47,7 +47,7 @@ $ pandoc -v
|
||||
bash: pandoc: command not found
|
||||
```
|
||||
|
||||
这个 toolbox 没有构建简历所需的程序。你可以通过使用 _dnf_ 安装工具来解决此问题。由于正在容器中运行,因此不会提示你输入 root 密码。
|
||||
这个 toolbox 没有构建简历所需的程序。你可以通过使用 `dnf` 安装工具来解决此问题。由于正在容器中运行,因此不会提示你输入 root 密码。
|
||||
|
||||
```
|
||||
$ sudo dnf groupinstall "Authoring and Publishing" -y && sudo dnf install pandoc make -y
|
||||
@ -63,7 +63,7 @@ $ ls BUILDS/
|
||||
resume.docx resume.html resume.pdf resume.rtf resume.txt
|
||||
```
|
||||
|
||||
运行 _exit_ 退出 toolbox。
|
||||
运行 `exit` 可以退出 toolbox。
|
||||
|
||||
```
|
||||
$ cd BUILDS/
|
||||
@ -80,21 +80,17 @@ bash: pandoc: command not found...
|
||||
resume.docx resume.html resume.pdf resume.rtf resume.txt
|
||||
```
|
||||
|
||||
你会在主目录中得到由 toolbox 创建的文件。toolbox 中安装的程序无法在外部访问。
|
||||
你会在主目录中得到由 toolbox 创建的文件。而在 toolbox 中安装的程序无法在外部访问。
|
||||
|
||||
### 提示和技巧
|
||||
|
||||
本介绍仅涉及 toolbox 的表明。还有一些其他提示,但是你也可以查看[官方文档][2]。
|
||||
本介绍仅涉及 toolbox 的表面。还有一些其他提示,但是你也可以查看[官方文档][2]。
|
||||
|
||||
* _Toolbox –help_ 会显示 Toolbox 的手册页
|
||||
* 你可以一次有多个 toolbox。使用 _toolbox create -c Toolboxname_ 和 _toolbox enter -c Toolboxname_。
|
||||
* Toolbox 使用 [Podman][4] 来完成繁重的工作。使用 _toolbox list_ 查找 Toolbox 创建的容器的 ID。Podman 可以使用这些 ID 来执行 _rm_ 和 _stop_ 之类的操作。 (你也可以在[此文章][5]中阅读有关 Podman 的更多信息。)
|
||||
* `toolbox –help` 会显示 Toolbox 的手册页。
|
||||
* 你可以一次有多个 toolbox。使用 `toolbox create -c Toolboxname` 和 `toolbox enter -c Toolboxname`。
|
||||
* Toolbox 使用 [Podman][4] 来完成繁重的工作。使用 `toolbox list` 可以查找 Toolbox 创建的容器的 ID。Podman 可以使用这些 ID 来执行 `rm` 和 `stop` 之类的操作。 (你也可以在[此文章][5]中阅读有关 Podman 的更多信息。)
|
||||
|
||||
|
||||
* * *
|
||||
|
||||
_照片出自 [Flickr][7] 的 [Florian Richter][6]。_
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://fedoramagazine.org/a-quick-introduction-to-toolbox-on-fedora/
|
||||
@ -102,7 +98,7 @@ via: https://fedoramagazine.org/a-quick-introduction-to-toolbox-on-fedora/
|
||||
作者:[Ryan Walter][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[geekpi](https://github.com/geekpi)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
校对:[wxy](https://github.com/wxy)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
@ -1,20 +1,21 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (geekpi)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: reviewer: (wxy)
|
||||
[#]: publisher: (wxy)
|
||||
[#]: url: (https://linux.cn/article-11650-1.html)
|
||||
[#]: subject: (Use the Window Maker desktop on Linux)
|
||||
[#]: via: (https://opensource.com/article/19/12/linux-window-maker-desktop)
|
||||
[#]: author: (Seth Kenlon https://opensource.com/users/seth)
|
||||
|
||||
在 Linux 上使用 Window Maker 桌面
|
||||
======
|
||||
本文是 24 天 Linux 桌面特别系列的一部分。与 Window Maker 一起倒退,它为如今的用户实现了老式 Unix NeXTSTEP 环境。
|
||||
> 本文是 24 天 Linux 桌面特别系列的一部分。让我们和 Window Maker 一起时光倒流,它为如今的用户实现了老式 Unix NeXTSTEP 环境。
|
||||
|
||||
![Penguin with green background][1]
|
||||
|
||||
在 Mac OS X 之前,有一个古怪的闭源 Unix 系统,称为 [NeXTSTEP][2]。Sun Microsystems 后来将 NeXTSTEP 的基础设为开放规范,这使其他项目可以创建许多免费开源的 NeXT 库和组件。GNUStep 实现了 NeXTSTEP 的大量库,[Window Maker][3] 实现了其桌面环境。
|
||||
在 Mac OS X 之前,有一个奇怪的闭源 Unix 系统,称为 [NeXTSTEP][2]。Sun Microsystems 后来将 NeXTSTEP 的底层设为开放规范,这使其它项目可以创建许多自由开源的 NeXT 库和组件。GNUStep 实现了许多 NeXTSTEP 库,而 [Window Maker][3] 实现了其桌面环境。
|
||||
|
||||
Window Maker 非常接近地模仿了 NeXTSTEP 桌面GUI,并提供了一些有趣东西来了解 80 年代末 90 年代初的 Unix 是什么样子。它还揭示了窗口管理器(例如 Fluxbox 和 Openbox)背后的一些基本概念。
|
||||
Window Maker 非常接近地模仿了 NeXTSTEP 桌面 GUI,并提供了一个有趣的视角,可以让人了解 80 年代末 90 年代初的 Unix 是什么样子的。它还揭示了窗口管理器(例如 Fluxbox 和 Openbox)背后的一些基本概念。
|
||||
|
||||
你可以从发行版的仓库中安装 Window Maker。要尝试它,请在安装完成后退出桌面会话。默认情况下,会话管理器(KDM、GDM、LightDM 或 XDM,这取决于你的设置)将继续将登录到默认桌面,因此登录时必须覆盖默认设置。
|
||||
|
||||
@ -26,20 +27,19 @@ Window Maker 非常接近地模仿了 NeXTSTEP 桌面GUI,并提供了一些有
|
||||
|
||||
![Selecting the Window Maker desktop in KDM][5]
|
||||
|
||||
### Window Maker dock
|
||||
### Window Maker 程序坞
|
||||
|
||||
默认情况下,Window Maker 桌面是空的,但每个角落都有几个 _dock_。像在 NeXTSTEP 中一样,在 Window Maker 中,在 dock 区,应用可最小化成图标后停靠,可创建启动器来快速访问常见应用,并且可运行微型的 ”dockapp“。
|
||||
|
||||
你可以在软件仓库中搜索 “dockapp” 来试用 dockapp。它们常常是网络和系统监控器、音频设置面板、时钟等。这是在 Fedora 上运行 Window Maker:
|
||||
默认情况下,Window Maker 桌面是空的,但每个角落都有几个*程序坞*。像在 NeXTSTEP 中一样,在 Window Maker 中,在程序坞区域,应用可最小化成图标后停靠,可创建启动器来快速访问常见应用,并且可运行微型的 “dockapp”。
|
||||
|
||||
你可以在软件仓库中搜索 “dockapp” 来试用 dockapp。它们常常是网络和系统监控器、音频设置面板、时钟等。这是在 Fedora 上运行的 Window Maker:
|
||||
|
||||
![Window Maker running on Fedora][6]
|
||||
|
||||
### 应用菜单
|
||||
|
||||
要访问应用菜单,请右键单击桌面上的任意位置。要再次关闭它,请单击鼠标右键。Window Maker 不是桌面环境。而是一个窗口管理器。它可以帮助你安排和管理窗口。它唯一捆绑的程序是 [WPrefs][7](或更常见的说法 Window Maker Preferences),它可帮助你配置常用设置,而应用菜单则提供对其他选项(包括主题)的访问。
|
||||
要访问应用菜单,请右键单击桌面上的任意位置。要关闭它,请再次单击鼠标右键。Window Maker 不是桌面环境(DE),而是一个窗口管理器(DM)。它可以帮助你安排和管理窗口。它唯一捆绑的程序是 [WPrefs][7](或更常见的说法 Window Maker 偏好),它可帮助你配置常用设置,而应用菜单则提供对其他选项(包括主题)的访问。
|
||||
|
||||
运行什么应用完全由你决定。在 Window Maker 中,你可以选择运行 KDE 应用、GNOME 应用以及不被视为任何其他不被视为桌面程序的应用。你可以创建自己的工作环境,并且可以使用 Window Maker 对其进行管理。
|
||||
运行什么应用完全由你决定。在 Window Maker 中,你可以选择运行 KDE 应用、GNOME 应用以及不被视为任何其他主流桌面应用的程序。你可以创建自己的工作环境,并且可以使用 Window Maker 对其进行管理。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
@ -48,7 +48,7 @@ via: https://opensource.com/article/19/12/linux-window-maker-desktop
|
||||
作者:[Seth Kenlon][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[geekpi](https://github.com/geekpi)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
校对:[wxy](https://github.com/wxy)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
@ -7,34 +7,31 @@
|
||||
[#]: via: (https://opensource.com/article/19/10/programming-bash-loops)
|
||||
[#]: author: (David Both https://opensource.com/users/dboth)
|
||||
|
||||
How to program with Bash: Loops
|
||||
Bash 编程教程之循环
|
||||
======
|
||||
Learn how to use loops for performing iterative operations, in the final
|
||||
article in this three-part series on programming with Bash.
|
||||
本文是 bash 编程系列(3 部分)的最后一篇,来学习使用循环执行迭代的操作。
|
||||
![arrows cycle symbol for failing faster][1]
|
||||
|
||||
Bash is a powerful programming language, one perfectly designed for use on the command line and in shell scripts. This three-part series, based on my [three-volume Linux self-study course][2], explores using Bash as a programming language on the command-line interface (CLI).
|
||||
Bash 是一种强大的用于命令行和 shell 脚本里的编程语言。本系列的三部分都是基于我的 [Linux 自学课程三卷][2] 写的,探索怎么用 CLI(command-line interface)进行 bash 编程。本系列的 [第一篇文章][3] 讨论了 bash 编程的一些简单命令行操作,如使用变量和控制操作符。[第二篇文章][4] 探讨了文件、字符串、数字等类型和各种各样在执行流中提供控制逻辑的的逻辑运算符,还有 bash 中不同种类的扩展。本文是第三篇(也是最后一篇),意在考察在各种迭代的操作中使用循环以及怎么合理控制循环。
|
||||
|
||||
The [first article][3] in this series explored some simple command-line programming with Bash, including using variables and control operators. The [second article][4] looked into the types of file, string, numeric, and miscellaneous logical operators that provide execution-flow control logic and different types of shell expansions in Bash. This third (and final) article examines the use of loops for performing various types of iterative operations and ways to control those loops.
|
||||
### 循环
|
||||
|
||||
### Loops
|
||||
我使用过的所有编程语言都有很多种循环结构来用来执行重复的操作。我经常使用 for 循环,然而我发现 while 和 until 循环也很有用处。
|
||||
|
||||
Every programming language I have ever used has at least a couple types of loop structures that provide various capabilities to perform repetitive operations. I use the for loop quite often but I also find the while and until loops useful.
|
||||
#### for 循环
|
||||
|
||||
#### for loops
|
||||
我的理解是,**for** 命令在 bash 中的实现比大部分语言灵活,因为它可以处理非数字的值;与之形成对比的是,诸如标准 C 语言的 **for** 循环只能处理数字类型的值。
|
||||
|
||||
Bash's implementation of the **for** command is, in my opinion, a bit more flexible than most because it can handle non-numeric values; in contrast, for example, the standard C language **for** loop can deal only with numeric values.
|
||||
|
||||
The basic structure of the Bash version of the **for** command is simple:
|
||||
Bash 版的 **for** 命令基本的结构很简单:
|
||||
|
||||
|
||||
```
|
||||
`for Var in list1 ; do list2 ; done`
|
||||
```
|
||||
|
||||
This translates to: "For each value in list1, set the **$Var** to that value and then perform the program statements in list2 using that value; when all of the values in list1 have been used, it is finished, so exit the loop." The values in list1 can be a simple, explicit string of values, or they can be the result of a command substitution (described in the second article in the series). I use this construct frequently.
|
||||
解释一下:“对于 list1 中的每一个值,把 **$Var** 设置为那个值,使用该值执行 list2 中的程序语句;list1 中的值都执行完后,整个循环结束,退出循环。” list1 中的值可以是一个简单的显式字符串值,也可以是一个命令执行后的结果(译注:**\` \`** 包含的命令执行的结果,本系列第二篇文章中有描述)。我经常使用这种结构。
|
||||
|
||||
To try it, ensure that **~/testdir** is still the present working directory (PWD). Clean up the directory, then look at a trivial example of the **for** loop starting with an explicit list of values. This list is a mix of alphanumeric values—but do not forget that all variables are strings and can be treated as such.
|
||||
确认 **~/testdir** 仍然是当前的工作目录(PWD)。删除目录下所有东西,来看下这个显式写出值列表的 **for** 循环的简单的示例。这个列表中的值是字母值 — 但是不要忘了,在 bash 中所有的变量都是 string 或者可以被当成 string 来处理。
|
||||
|
||||
|
||||
```
|
||||
@ -50,7 +47,7 @@ d
|
||||
4
|
||||
```
|
||||
|
||||
Here is a bit more useful version with a more meaningful variable name:
|
||||
给变量赋予更有意义的名字,变成前面版本的进阶版:
|
||||
|
||||
|
||||
```
|
||||
@ -64,7 +61,7 @@ Department Administration
|
||||
Department Research
|
||||
```
|
||||
|
||||
Make some directories (and show some progress information while doing so):
|
||||
创建几个目录(创建时显示一些处理信息):
|
||||
|
||||
|
||||
```
|
||||
@ -87,9 +84,9 @@ drwxrwxr-x 2 student student 4096 Apr 8 15:45 Research
|
||||
drwxrwxr-x 2 student student 4096 Apr 8 15:45 Sales
|
||||
```
|
||||
|
||||
The **$Dept** variable must be enclosed in quotes in the **mkdir** statement; otherwise, two-part department names (such as "Information Technology") will be treated as two separate departments. That highlights a best practice I like to follow: all file and directory names should be a single word. Although most modern operating systems can deal with spaces in names, it takes extra work for sysadmins to ensure that those special cases are considered in scripts and CLI programs. (They almost certainly should be considered, even if they're annoying because you never know what files you will have.)
|
||||
在 **mkdir** 语句中 **$Dept** 变量必须用引号包裹起来;否则名字中间有空格(如 `Information Technology`)会被当做两个独立的目录处理。我一直信奉的一条实践规则:所有的文件和目录都应该为一个单词(中间没有空格)。虽然大部分现代的操作系统可以处理名字中间有空格的情况,但是系统管理员需要花费额外的精力去确保脚本和 CLI 程序能正确处理这些特例。(尽管你因为不知道将要处理什么样的文件而烦恼,但是脚本和 CLI 应该已经把这些特例考虑在内了。)
|
||||
|
||||
So, delete everything in **~/testdir**—again—and do this one more time:
|
||||
再次删除 **~/testdir** 下的所有东西 — 再运行一次下面的命令:
|
||||
|
||||
|
||||
```
|
||||
@ -114,11 +111,9 @@ drwxrwxr-x 2 student student 4096 Apr 8 15:52 Research
|
||||
drwxrwxr-x 2 student student 4096 Apr 8 15:52 Sales
|
||||
```
|
||||
|
||||
Suppose someone asks for a list of all RPMs on a particular Linux computer and a short description of each. This happened to me when I worked for the State of North Carolina. Since open source was not "approved" for use by state agencies at that time, and I only used Linux on my desktop computer, the pointy-haired bosses (PHBs) needed a list of each piece of software that was installed on my computer so that they could "approve" an exception.
|
||||
假设现在有个需求,需要列出一台 Linux 机器上所有的 RPM 包并对每个包附上简短的描述。我为North Carolina 州工作的时候,曾经遇到过这种需求。由于当时州代理处不允许使用开源的工具,而且我对 Linux 不够熟悉,对技术一窍不通的老板(PHB)需要我列出我计算机上安装的所有软件,这样他们就可以“查看“计算机上有没有安装异常的软件了。
|
||||
|
||||
How would you approach that? Here is one way, starting with the knowledge that the **rpm –qa** command provides a complete description of an RPM, including the two items the PHBs want: the software name and a brief summary.
|
||||
|
||||
Build up to the final result one step at a time. First, list all RPMs:
|
||||
你怎么实现它?有一种方法是,已知 **rpm –qa** 命令提供了 RPM 包的完整描述,包括 PHB 想要的东西:软件名称和概要描述。一步步执行出最后的结果。首先,列出所有的 RPM 包:
|
||||
|
||||
|
||||
```
|
||||
@ -136,7 +131,7 @@ libfontenc-1.1.3-9.fc29.x86_64
|
||||
<snip>
|
||||
```
|
||||
|
||||
Add the **sort** and **uniq** commands to sort the list and print the unique ones (since it's possible that some RPMs with identical names are installed):
|
||||
用 **sort** 和 **uniq** 命令对列表进行排序和打印去重后的结果(有些已安装的 RPM 包具有相同的名字:)
|
||||
|
||||
|
||||
```
|
||||
@ -154,14 +149,14 @@ abrt-addon-vmcore-2.11.0-1.fc29.x86_64
|
||||
<snip>
|
||||
```
|
||||
|
||||
Since this gives the correct list of RPMs you want to look at, you can use this as the input list to a loop that will print all the details of each RPM:
|
||||
以上命令得到了想要的 RPM 列表,因此你可以把这个列表作为一个循环的输入信息,循环最终会打印每个 RPM 包的详细信息:
|
||||
|
||||
|
||||
```
|
||||
`[student@studentvm1 testdir]$ for RPM in `rpm -qa | sort | uniq` ; do rpm -qi $RPM ; done`
|
||||
[student@studentvm1 testdir]$ for RPM in `rpm -qa | sort | uniq` ; do rpm -qi $RPM ; done
|
||||
```
|
||||
|
||||
This code produces way more data than you want. Note that the loop is complete. The next step is to extract only the information the PHBs requested. So, add an **egrep** command, which is used to select **^Name** or **^Summary**. The carat (**^**) specifies the beginning of the line; thus, any line with Name or Summary at the beginning of the line is displayed.
|
||||
这段代码产出了多余的信息。当循环结束后, 下一步就是提取出 PHB 需要的信息。因此,添加一个 **egrep** 命令用来搜索匹配 **^Name** 或 **^Summary** 的行。脱字符(^)表示行首,整个命令表示显示所有以 Name 或 Summary 开头的行。
|
||||
|
||||
|
||||
```
|
||||
@ -179,42 +174,38 @@ Summary : Automatic bug detection and reporting tool
|
||||
<snip>
|
||||
```
|
||||
|
||||
You can try **grep** instead of **egrep** in the command above, but it will not work. You could also pipe the output of this command through the **less** filter to explore the results. The final command sequence looks like this:
|
||||
在上面的命令中你可以试试用 **grep** 代替 **egrep** ,你会发现用 **grep** 不能得到正确的结果。你可以通过管道把命令结果用 **less** 过滤下。最终命令像这样:
|
||||
|
||||
|
||||
```
|
||||
`[student@studentvm1 testdir]$ for RPM in `rpm -qa | sort | uniq` ; do rpm -qi $RPM ; done | egrep -i "^Name|^Summary" > RPM-summary.txt`
|
||||
[student@studentvm1 testdir]$ for RPM in `rpm -qa | sort | uniq` ; do rpm -qi $RPM ; done | egrep -i "^Name|^Summary" > RPM-summary.txt
|
||||
```
|
||||
|
||||
This command-line program uses pipelines, redirection, and a **for** loop—all on a single line. It redirects the output of your little CLI program to a file that can be used in an email or as input for other purposes.
|
||||
这个命令行程序在同一行中用到了管道、重定向和 **for** 循环。它把你 CLI 程序的结果重定向到了一个文件,这个文件可以在邮件中使用或在其他地方作为输入使用。这个一次一步构建程序的过程让你能看到每步的结果,以此来确保整个程序以你期望的流程进行且输出你想要的结果。
|
||||
|
||||
This process of building up the program one step at a time allows you to see the results of each step and ensure that it is working as you expect and provides the desired results.
|
||||
PHB 最终收到了超过 1900 个独立 RPM 包的清单,我严重怀疑根本就没人去读它。我给了他们想要的东西,没有从他们嘴里听到过任何关于 RPM 包的信息。
|
||||
|
||||
From this exercise, the PHBs received a list of over 1,900 separate RPM packages. I seriously doubt that anyone read that list. But I gave them exactly what they asked for, and I never heard another word from them about it.
|
||||
### 其他循环
|
||||
|
||||
### Other loops
|
||||
|
||||
There are two more types of loop structures available in Bash: the **while** and **until** structures, which are very similar to each other in both syntax and function. The basic syntax of these loop structures is simple:
|
||||
Bash 中还有两种其他类型的循环结构:**while** 和 **until** 结构,两者在语法和功能上都类似。这些循环结构的基础语法很简单:
|
||||
|
||||
|
||||
```
|
||||
`while [ expression ] ; do list ; done`
|
||||
while [ expression ] ; do list ; done
|
||||
```
|
||||
|
||||
and
|
||||
逻辑解释:表达式(expression)结果为 true 时,执行程序语句 `list`。表达式结果为 false 时,退出循环。
|
||||
|
||||
|
||||
```
|
||||
`until [ expression ] ; do list ; done`
|
||||
until [ expression ] ; do list ; done
|
||||
```
|
||||
|
||||
The logic of the first reads: "While the expression evaluates as true, execute the list of program statements. When the expression evaluates as false, exit from the loop." And the second: "Until the expression evaluates as true, execute the list of program statements. When the expression evaluates as true, exit from the loop."
|
||||
逻辑解释:直到表达式的结果为 true,执行程序语句 `list`。当表达式结果为 true 时,退出循环。
|
||||
|
||||
#### While loop
|
||||
#### While 循环
|
||||
|
||||
The **while** loop is used to execute a series of program statements while (so long as) the logical expression evaluates as true. Your PWD should still be **~/testdir**.
|
||||
|
||||
The simplest form of the **while** loop is one that runs forever. The following form uses the true statement to always generate a "true" return code. You could also use a simple "1"—and that would work just the same—but this illustrates the use of the true statement:
|
||||
**while** 循环用于当逻辑表达式结果为 true 时执行一系列程序语句。假设你的 PWD 仍是 **~/testdir** 。最简单的 **while** 循环会一直运行下去。下面格式的条件语句永远以 `true` 作为返回。你也可以用简单的 `1` 代替 `true` — 结果一样 — 但是这解释了 true 表达式的用法。
|
||||
|
||||
|
||||
```
|
||||
@ -232,9 +223,9 @@ The simplest form of the **while** loop is one that runs forever. The following
|
||||
[student@studentvm1 testdir]$
|
||||
```
|
||||
|
||||
This CLI program should make more sense now that you have studied its parts. First, it sets **$X** to zero in case it has a value left over from a previous program or CLI command. Then, since the logical expression **[ true ]** always evaluates to 1, which is true, the list of program instructions between **do** and **done** is executed forever—or until you press **Ctrl+C** or otherwise send a signal 2 to the program. Those instructions are an arithmetic expansion that prints the current value of **$X** and then increments it by one.
|
||||
既然你已经学了 CLI 的各部分知识,那就让它变得更有用处。首先,为了防止变量 **$X** 在前面的程序或 CLI 命令执行后有遗留的值,设置 **$X** 的值为 0。然后,因为逻辑表达式 **[ true ]** 的结果永远是 1,即 true,在 **do** 和 **done** 中间的程序指令列表会一直执行 — 或者直到你按下 **Ctrl+C** 抑或发送一个 `signal 2` 给程序。那些程序指令是算法扩展,用来打印变量 **$X** 当前的值并加 1.
|
||||
|
||||
One of the tenets of [_The Linux Philosophy for Sysadmins_][5] is to strive for elegance, and one way to achieve elegance is simplicity. You can simplify this program by using the variable increment operator, **++**. In the first instance, the current value of the variable is printed, and then the variable is incremented. This is indicated by placing the **++** operator after the variable:
|
||||
[系统管理员的 Linux 哲学][5] 的信条之一是追求优雅,实现优雅的一种方式就是简化。你可以用操作符 **++** 来简化这个程序。在第一个例子中,变量当前的值被打印出来,然后变量的值增加了。可以在变量后加一个 **++** 来表示这个逻辑:
|
||||
|
||||
|
||||
```
|
||||
@ -251,9 +242,7 @@ One of the tenets of [_The Linux Philosophy for Sysadmins_][5] is to strive for
|
||||
9
|
||||
```
|
||||
|
||||
Now delete **| head** from the end of the program and run it again.
|
||||
|
||||
In this version, the variable is incremented before its value is printed. This is specified by placing the **++** operator before the variable. Can you see the difference?
|
||||
现在删掉程序最后的 **| head** 再运行一次。在这个版本中,变量在值被打印之前就自增了。这是通过在变量之前添加 **++** 操作符实现的。你能看出区别吗?
|
||||
|
||||
|
||||
```
|
||||
@ -269,9 +258,7 @@ In this version, the variable is incremented before its value is printed. This i
|
||||
9
|
||||
```
|
||||
|
||||
You have reduced two statements into a single one that prints the value of the variable and increments that value. There is also a decrement operator, **\--**.
|
||||
|
||||
You need a method for stopping the loop at a specific number. To accomplish that, change the true expression to an actual numeric evaluation expression. Have the program loop to 5 and stop. In the example code below, you can see that **-le** is the logical numeric operator for "less than or equal to." This means: "So long as **$X** is less than or equal to 5, the loop will continue. When **$X** increments to 6, the loop terminates."
|
||||
你已经把打印变量的值和自增简化到了一条语句。类似 **++** 操作符, 也有 **--** 操作符。你需要一个在循环到某个特定数字时终止循环的方法。把 true 表达式换成一个数字比较表达式来实现它。写一个循环到 5 终止的程序。在下面的示例代码中,你可以看到 **-le** 是 ”小于或等于“ 的数字逻辑操作符。整个语句的意思:只要 **$X** 的值小于或等于 5,循环就一直运行。当 **$X** 增加到 6时,循环终止。
|
||||
|
||||
|
||||
```
|
||||
@ -285,9 +272,11 @@ You need a method for stopping the loop at a specific number. To accomplish that
|
||||
[student@studentvm1 ~]$
|
||||
```
|
||||
|
||||
#### Until loop
|
||||
#### Until 循环
|
||||
|
||||
The **until** command is very much like the **while** command. The difference is that it will continue to loop until the logical expression evaluates to "true." Look at the simplest form of this construct:
|
||||
|
||||
|
||||
*until** 命令非常像 **while** 命令。不同之处是,它直到逻辑表达式的值是 `true` 之前,会一直循环。看一下这种结构最简单的格式:
|
||||
|
||||
|
||||
```
|
||||
@ -305,7 +294,7 @@ The **until** command is very much like the **while** command. The difference is
|
||||
[student@studentvm1 ~]$
|
||||
```
|
||||
|
||||
It uses a logical comparison to count to a specific value:
|
||||
它用一个逻辑比较表达式来数到一个特定的值:
|
||||
|
||||
|
||||
```
|
||||
@ -324,13 +313,9 @@ It uses a logical comparison to count to a specific value:
|
||||
[student@studentvm1 ~]$
|
||||
```
|
||||
|
||||
### Summary
|
||||
### 总结:
|
||||
|
||||
This series has explored many powerful tools for building Bash command-line programs and shell scripts. But it has barely scratched the surface on the many interesting things you can do with Bash; the rest is up to you.
|
||||
|
||||
I have discovered that the best way to learn Bash programming is to do it. Find a simple project that requires multiple Bash commands and make a CLI program out of them. Sysadmins do many tasks that lend themselves to CLI programming, so I am sure that you will easily find tasks to automate.
|
||||
|
||||
Many years ago, despite being familiar with other shell languages and Perl, I made the decision to use Bash for all of my sysadmin automation tasks. I have discovered that—sometimes with a bit of searching—I have been able to use Bash to accomplish everything I need.
|
||||
本系列探讨了构建 bash 命令行程序和 shell 脚本的很多强大的工具。但是这仅仅是你能用 Bash 做的很多有意思的事中的冰山一角,接下来就看你的了。我发现学习 Bash 编程最好的方法就是实践。找一个需要多个 Bash 命令的简单项目然后写一个 CLI 程序。系统管理员们做了很多工作让任务变成 CLI 编程,因此我确信你很容易能找到自动化的任务。很多年前,尽管我对其他的 Shell 语言和 Perl 很熟悉,但还是决定用 Bash 做所有系统管理员的自动化任务。我发现 — 有时稍微搜索一下 — 我可以用 Bash 实现我需要的所有事情。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
@ -338,7 +323,7 @@ via: https://opensource.com/article/19/10/programming-bash-loops
|
||||
|
||||
作者:[David Both][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
译者:[lxbwolf](https://github.com/lxbwolf)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
@ -1,5 +1,5 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: ( )
|
||||
[#]: translator: (hj24)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
|
Loading…
Reference in New Issue
Block a user