Merge pull request #576 from geekpi/master

[Translated] 10 Useful Chaining Operators in Linux with Practical Exampl...
This commit is contained in:
Xingyu.Wang 2013-12-16 05:40:31 -08:00
commit a915cfdf7e
2 changed files with 158 additions and 162 deletions

View File

@ -1,162 +0,0 @@
Translating-------------geekpi
10 Useful Chaining Operators in Linux with Practical Examples
================================================================================
Chaining of Linux commands means, combining several commands and make them execute based upon the behaviour of operator used in between them. Chaining of commands in Linux, is something like you are writing [short shell scripts][1] at the shell itself, and executing them from the terminal directly. Chaining makes it possible to automate the process. Moreover, an unattended machine can function in a much systematic way with the help of chaining operators.
![10 Chaining Operators in Linux](http://www.tecmint.com/wp-content/uploads/2013/12/Chaining-Operators-in-Linux.png)
*10 Chaining Operators in Linux*
This Article aims at throwing light on frequently used **command­-chaining operators**, with short descriptions and corresponding examples which surely will increase your productivity and lets you write short and meaningful codes beside reducing system load, at times.
### 1. Ampersand Operator (&) ###
The function of **&** is to make the command run in background. Just type the command followed with a white space and **&**. You can execute more than one command in the background, in a single go.
Run one command in the background:
tecmint@localhost:~$ ping ­c5 www.tecmint.com &
Run two command in background, simultaneously:
root@localhost:/home/tecmint# apt-get update & apt-get upgrade &
### 2. semi-colon Operator (;) ###
The semi-colon operator makes it possible to run, several commands in a single go and the execution of command occurs sequentially.
root@localhost:/home/tecmint# apt-get update ; apt-get upgrade ; mkdir test
The above command combination will first execute **update** instruction, then **upgrade** instruction and finally will create a **test** directory under the current working directory.
### 3. AND Operator (&&) ###
The **AND Operator (&&)** would execute the second command only, if the execution of first command fails, i.e., the exit status of the first command is **1**. This command is very useful in checking the execution status of last command.
For example, I want to visit website **tecmint.com** using **[links command][2]**, in terminal but before that I need to check if the host is **live** or **not**.
root@localhost:/home/tecmint# ping -c3 www.tecmint.com && links www.tecmint.com
### 4. OR Operator (||) ###
The **OR Operator (||)** is much like an **else** statement in programming. The above operator allow you to execute second command only if the execution of first command fails, i.e., the exit status of first command is **1**.
For example, I want to execute **apt-get update** from non-root account and if the first command fails, then the second **links www.tecmint.com** command will execute.
tecmint@localhost:~$ apt-get update || links tecmint.com
In the above command, since the **user** was not allowed to **update** system, it means that the exit status of first command is **1** and hence the last command **links tecmint.com** gets executed.
What if the first command is executed successfully, with an exit status **0**? Obviously! Second command wont execute.
tecmint@localhost:~$ mkdir test || links tecmint.com
Here, the user creates a folder **test** in his home directory, for which user is permitted. The command executed successfully giving an exit status **0** and hence the last part of the command is not executed.
### 5. NOT Operator (!) ###
The **NOT Operator (!)** is much like an **except** statement. This command will execute all except the condition provided. To understand this, create a directory **tecmint** in your home directory and **cd** to it.
tecmint@localhost:~$ mkdir tecmint
tecmint@localhost:~$ cd tecmint
Next, create several types of files in the folder **tecmint**.
tecmint@localhost:~/tecmint$ touch a.doc b.doc a.pdf b.pdf a.xml b.xml a.html b.html
See weve created all the new files within the folder **tecmint**.
tecmint@localhost:~/tecmint$ ls
a.doc a.html a.pdf a.xml b.doc b.html b.pdf b.xml
Now delete all the files except **html** file all at once, in a smart way.
tecmint@localhost:~/tecmint$ rm -r !(*.html)
Just to verify, last execution. List all of the available files using [ls command][3].
tecmint@localhost:~/tecmint$ ls
a.html b.html
### 6. AND OR operator (&& ||) ###
The above operator is actually a combination of **AND** and **OR** Operator. It is much like an **if-else** statement.
For example, lets do ping to **tecmint.com**, if success echo **Verified** else echo **Host Down**.
tecmint@localhost:~/tecmint$ ping -c3 www.tecmint.com && echo "Verified" || echo "Host Down"
#### Sample Output ####
PING www.tecmint.com (212.71.234.61) 56(84) bytes of data.
64 bytes from www.tecmint.com (212.71.234.61): icmp_req=1 ttl=55 time=216 ms
64 bytes from www.tecmint.com (212.71.234.61): icmp_req=2 ttl=55 time=224 ms
64 bytes from www.tecmint.com (212.71.234.61): icmp_req=3 ttl=55 time=226 ms
--- www.tecmint.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2001ms
rtt min/avg/max/mdev = 216.960/222.789/226.423/4.199 ms
Verified
Now, disconnect your internet connection, and try same command again.
tecmint@localhost:~/tecmint$ ping -c3 www.tecmint.com && echo "verified" || echo "Host Down"
#### Sample Output ####
ping: unknown host www.tecmint.com
Host Down
### 7. PIPE Operator (|) ###
This **PIPE** operator is very useful where the output of first command acts as an input to the second command. For example, pipeline the output of **ls -l** to **less** and see the output of the command.
tecmint@localhost:~$ ls -l | less
### 8. Command Combination Operator {} ###
Combine two or more commands, the second command depends upon the execution of the first command.
For example, check if a file **xyz.txt** and **xyz1.txt** is available under my **Downloads** directory or not, and output corresponding output.
tecmint@localhost:~$ [ -f /home/tecmint/Downloads/xyz.txt ] || echo “The file does not exist”
tecmint@localhost:~$ [ -f /home/tecmint/Downloads/xyz1.txt ] || echo “The file does not exist”
“The file does not exist”
### 9. Precedence Operator () ###
The Operator makes it possible to execute command in precedence order.
Command_x1 &&Command_x2 || Command_x3 && Command_x4.
In the above pseudo command, what if the **Command_x1** fails? Neither of the **Command_x2**, **Command_x3**, **Command_x4** would executed, for this we use **Precedence Operator**, as:
(Command_x1 &&Command_x2) || (Command_x3 && Command_x4)
In the above pseudo command, if **Command_x1** fails, **Command_x2** also fails but Still **Command_x3** and **Command_x4** executes depends upon exit status of **Command_x3**.
### 10. Concatenation Operator (\) ###
The **Concatenation Operator (\)** as the name specifies, is used to concatenate large commands over several lines in the shell. For example, The below command will open text file **test(1).txt**.
tecmint@localhost:~/Downloads$ nano test\(1\).txt
Thats all for now. I am coming up with another interesting article very soon. Till then Stay tuned, healthy and connected to **Tecmint**. Dont forget to give your Valuable feedback in our comment section.
--------------------------------------------------------------------------------
via: http://www.tecmint.com/chaining-operators-in-linux-with-practical-examples/
译者:[译者ID](https://github.com/译者ID) 校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
[1]:http://www.tecmint.com/category/bash-shell/
[2]:http://www.tecmint.com/command-line-web-browsers/
[3]:http://www.tecmint.com/15-basic-ls-command-examples-in-linux/

View File

@ -0,0 +1,158 @@
示例说明10个Linux中有用打的链接操作符
================================================================================
Linux命令中的链接的意思是结合几个命令并基于它们之间操作符的行为执行。Linux中的链接命令有些像你在shell中写[短小的shell脚本],并直接在终端中执行。链接使得自动处理变得可能。不仅如此,一个无人看管的机器在链接操作符的帮助下如此有条理地运行。
![Linux中的10个链接操作符](http://www.tecmint.com/wp-content/uploads/2013/12/Chaining-Operators-in-Linux.png)
*Linux中的10个链接操作符*
本文旨在常用的**链接操作符**,并作简短的描述和相关的肯定能提高你生产力的例子,除了有时能降低系统负载外还能让你写出简短有意义的代码。
### 1. 和号操作符 (&) ###
**&**‘的作用是使命令在后台运行。只要在命令后面跟上一个空格和 **&**。你可以一口气在后台运行多个命令。
在后台运行一个命令:
tecmint@localhost:~$ ping ­c5 www.tecmint.com &
同时在后台运行两个命令:
root@localhost:/home/tecmint# apt-get update & apt-get upgrade &
### 2. 分号操作符 (;) ###
分号操作符使你可以一口气运行几个命令,命令顺序执行。
root@localhost:/home/tecmint# apt-get update ; apt-get upgrade ; mkdir test
创建了一个‘**test**‘文件夹
### 3. 与操作符 (&&) ###
如果第一个命令执行成功(译者注: 原文为“if the execution of first command fails”译者认为与上下文意思不同)**与操作符 (&&)**会执行第二个命令,也就是说,第一个命令退出状态是**1**。这个命令在检查最后一个命令的执行状态时很有用。
比如,我想使用**[links command][2]**在终端中访问网站**tecmint.com**,但在这之前我需要检查主机是否**在线**或者**不在线**。
root@localhost:/home/tecmint# ping -c3 www.tecmint.com && links www.tecmint.com
### 4. 或操作符 (||) ###
**或操作符 (||)**很像编程中的**else**语句。上面的操作符允许你在第一个命令失败的情况下执行第二个命令,也就是说,第一个命令的退出状态是**1**。
举例来说我想要在非root帐户中执行**apt-get update**‘,如果第一个命令失败了,接着会执行第二个命令‘**links www.tecmint.com**‘。
tecmint@localhost:~$ apt-get update || links tecmint.com
上面的命令中,由于**用户**不允许**更新**系统,这意味着第一个命令的退出状态是’**1**,因此最后一个命令‘**links tecmint.com**‘会执行。
如果第一个命令成功执行并且退出状态是‘**0**‘呢?很明显的,第二个命令不会执行。
tecmint@localhost:~$ mkdir test || links tecmint.com
这里,用户在家目录创建了一个‘**test**‘文件夹,这是被允许的。命令成功的执行,退出状态是‘**0**,因此,最后的命令不会执行。
### 5. 非操作符 (!) ###
**非操作符 (!)**很像**except**语句。这个命令会执行除了提供的条件外的所有的语句。要理解这点,在你的家目录创建一个目录‘**tecmint**‘,并‘**cd**‘到它这里。
tecmint@localhost:~$ mkdir tecmint
tecmint@localhost:~$ cd tecmint
接下来,在文件夹‘**tecmint**‘下创建不同类型的文件。
tecmint@localhost:~/tecmint$ touch a.doc b.doc a.pdf b.pdf a.xml b.xml a.html b.html
看一下我们在文件夹‘**tecmint**‘创建的新文件。
tecmint@localhost:~/tecmint$ ls
a.doc a.html a.pdf a.xml b.doc b.html b.pdf b.xml
用一种聪明的办法马上删除除了 **html**‘之外的所有文件。
tecmint@localhost:~/tecmint$ rm -r !(*.html)
验证一下上次的执行结果,使用[ls 命令][3]列出可见所有文件。
tecmint@localhost:~/tecmint$ ls
a.html b.html
### 6. 与 或 操作符 (&& ||) ###
上面的操作符实际上是‘**与**‘和‘**或**‘操作符的组合。它很像‘**if-else**‘语句。
比如让我们ping **tecmint.com**,如果成功打印‘**已验证**‘,否则打印‘**主机故障**‘。
tecmint@localhost:~/tecmint$ ping -c3 www.tecmint.com && echo "Verified" || echo "Host Down"
#### 示例输出 ####
PING www.tecmint.com (212.71.234.61) 56(84) bytes of data.
64 bytes from www.tecmint.com (212.71.234.61): icmp_req=1 ttl=55 time=216 ms
64 bytes from www.tecmint.com (212.71.234.61): icmp_req=2 ttl=55 time=224 ms
64 bytes from www.tecmint.com (212.71.234.61): icmp_req=3 ttl=55 time=226 ms
--- www.tecmint.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2001ms
rtt min/avg/max/mdev = 216.960/222.789/226.423/4.199 ms
Verified
现在,断开我们现在的网络连接诶,再试一下相同的命令。
tecmint@localhost:~/tecmint$ ping -c3 www.tecmint.com && echo "verified" || echo "Host Down"
#### 实例输出 ####
ping: unknown host www.tecmint.com
Host Down
### 7. 管道操作符 (|) ###
**PIPE**在第一个命令的输出作为第二个命令的输入时很有用。比如,‘**ls -l**‘的输出通过管道到‘**less**‘,并看一下输出。
tecmint@localhost:~$ ls -l | less
### 8. 命令合并操作符 {} ###
合并两个或多个命令,第二个命令依赖于第一个命令的执行。
比如,检查一下文件‘**xyz.txt**‘和‘**xyz1.txt**‘是否在**Downloads**目录下,并输出相关的输出。
tecmint@localhost:~$ [ -f /home/tecmint/Downloads/xyz.txt ] || echo “The file does not exist”
tecmint@localhost:~$ [ -f /home/tecmint/Downloads/xyz1.txt ] || echo “The file does not exist”
“The file does not exist”
### 9. 优先操作符 () ###
这个操作符可以让命令以优先顺序执行。
Command_x1 &&Command_x2 || Command_x3 && Command_x4.
在上面的伪代码中,如果**Command_x1**执行失败了会怎么样,**Command_x2**, **Command_x3**, **Command_x4**没有一个会自行,对于这种,我们使用**优先操作符**
(Command_x1 &&Command_x2) || (Command_x3 && Command_x4)
在上面的伪代码中,如果**Command_x1**执行失败,**Command_x2**同样失败,但是**Command_x3**会继续执行, **Command_x4**会依赖于 **Command_x3**的退出状态。
### 10. 连接符 (\) ###
**连接符 (\)**如它名字所说被用于连接shell中跨越多行的命令。比如下面的命令会打开文本文件**test(1).txt**。
tecmint@localhost:~/Downloads$ nano test\(1\).txt
今天就到这里,我会近日开始另外一个有趣的文章。不要走开,继续关注**Tecmint**。不要忘记在评论栏里提出有价值的反馈。
--------------------------------------------------------------------------------
via: http://www.tecmint.com/chaining-operators-in-linux-with-practical-examples/
译者:[geekpi](https://github.com/geekpi) 校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
[1]:http://www.tecmint.com/category/bash-shell/
[2]:http://www.tecmint.com/command-line-web-browsers/
[3]:http://www.tecmint.com/15-basic-ls-command-examples-in-linux/