A detailed explanation of the execution sequence of multi command logic in shell script

Time:2020-9-16

Linux can use semicolon “;”, double and “& &” and double vertical “|” to connect multiple commands. The single “&” symbol is also a command connection symbol, but it puts the previous command into the background for execution, so it can realize the parallel execution of commands in disguise.

1. Semicolon “;”

command1 ; command2

There is no logical relationship between commands. The commands connected with semicolon will be executed from front to back in order, but there is no logical relationship between the commands at both ends of semicolon. All written commands will be executed eventually. Even if the command in front of semicolon is wrong, the following command will not be affected.


[[email protected] ~]# ls das;echo "hdakl"
ls: cannot access das: No such file or directory

hdakl

2.&&

command1  &&  command2

Logic and. &&The connected commands are executed from front to back in order, but only if command1 is executed correctly can command2 be executed. If command1 is not executed correctly, command2 is not executed. In Bash, the pre-defined variable “$?” is used to determine whether the command is executed correctly. If the value of “$?” is 0, the previous command is executed correctly, and any other value indicates that the command is not executed correctly. “


[[email protected] ~]# echo "hdakl" && ls ds 
hdakl
ls: cannot access ds: No such file or directory
[[email protected] ~]# ls das && echo "hdakl"
ls: cannot access das: No such file or directory

3.||

command1 || command2

Logical or. ||The connected commands are executed from front to back in order, but only if command1 is not executed correctly, command2 will not be executed if command1 is executed correctly. ||And & & are short-circuit symbols. There is a logical relationship between the commands left and right of the symbol.


[[email protected] ~]# ls das || echo "hdakl" 
ls: cannot access das: No such file or directory
hdakl
[[email protected] ~]# echo "hdakl" || ls ds  
hdakl

Generally, when you want to use the combination of & & | and |, you can basically use logic first and then logic or: command1 & & command2 | command3. Because in practice, both command2 and command3 should be commands that you want to execute. If command1 is executed correctly, $? Is equal to 0. Execute command2. Depending on the situation, execute command3. If command1 is executed incorrectly, $? Is not equal to 0. Therefore, command2 is not executed. According to the non-0 value of $? It is judged that the command on the right side of |, should be executed.

The understanding method of popular points is based on semantic judgment. “If… On… Otherwise… On…” statements use “CMD1 & & cmd2 | cmd3”, “if not… Then… Otherwise… Use”! CMD1 & & cmd2 | cmd3 “.

For example, if user user1 exists, it shows that the user already exists; otherwise, the user is added.


[[email protected] tmp]# id user1 && echo "user1 exists" || useradd user1

If user user2 does not exist, this user is added; otherwise, the user already exists.


[[email protected] tmp]# !id user2 && useradd user2 || echo "user2 exists"

If user user3 does not exist, add the user and set its password to the user name itself. Otherwise, the user already exists.


[[email protected] tmp]# !id user3 && useradd user3 && echo "user3" | passwd --stdin user3 || echo "user3 exists"

4.&

command1 &
command1 & command2

&It means that the command in front of it will be executed in the background, and it will immediately return to the bash environment so that users can continue to interact with bash. If the & symbol connects two commands, the command in front of it is put into the background, and the latter command is executed immediately. Therefore, it can be simply considered that the two commands are executed in parallel, and there is no logical relationship between the commands at both ends.

It should be noted that in the bash environment of the terminal, the background processes in the subshell are not controlled by the terminal. When the terminal is shut down, it will be attached to the init / SYSTEMd process. Therefore, if you exit the terminal or script shell environment, you cannot interrupt these background processes. For example:

[ root[email protected]  ~]#(sleep 10) ා execute on terminal 1 and close the terminal immediately

[ [email protected]  ~]#PS aux | grep sleep [P] ා capture sleep process on Terminal 2
root 5732 0.0 0.0 107892 624 ? S 00:28 0:00 sleep 10

Note the “?” in the PS result, which indicates a non terminal process, that is, it is detached from the terminal.

Knowledge point supplement: shell command execution order

When executing a command, sometimes it depends on the success of the previous command. For example, suppose you want to copy all the files in one directory to another, and then delete all the files in the source directory. Before deleting, you want to be sure that the copy is successful, otherwise you may lose the file

In this paper, we will discuss:

Command execution control.

Command combination.

&&Use of:

&&We can solve the problem mentioned in the background above. Only when the previous instruction is successfully executed can the subsequent instruction be executed.

       Usage syntax:

Command 1 & Command 2

Equivalent to logic and operation:

condition1 && condition2

Only when condition1 is true can condition2 be judged

Example:

Requirements: copy / tmp/ nginx.conf To / etc / nginx/ nginx.conf Rear handle / tmp/ nginx.conf delete

cp /tmp/nginx.conf /etc/nginx/nginx.conf&&rm -rf /tmp/nginx.conf

||Use of:

||When the order in front of |, is successfully executed, the command behind |, is not executed

Usage syntax:

Command 1 | Command 2

Logical or thinking.

Example:

Requirements: copy / tmp/ nginx.conf To / etc / nginx/ nginx.conf Otherwise / tmp/ nginx.conf Copy to / usr / nginx/ nginx.conf

cp /tmp/nginx.conf /etc/nginx/nginx.conf||cp /tmp/nginx.conf /usr/nginx/nginx.conf
In order to execute a set of commands in the current s hel, you can separate each command with a command separator and enclose all commands in parentheses ().

Grammar:

(command 1; Command 2;..)

Example: when it fails, email yourself and exit

[[email protected] shell_protest]# cat co.sh
#!/bin/bash
​
false||echo "it is fail"|mail nick&&exit
#Special case: when it fails, sending email also fails, you can't quit the program
false||echo "it is fail"|mail nick||exit
#Exception: when it fails, the email is successful, but it can't exit the program
false||(echo "it is fail"|mail nick;exit)
#Therefore, using () to combine commands is a good choice

Summary:

When writing shell scripts, using & & and |, is very useful for constructing judgment statements. If you want to do not execute the following command when the previous command fails, the method described in this paper is very simple and effective. With this method, you can control the execution of the following command according to the return value of the previous command.

In this article, we will introduce the detailed method of multi command logical execution sequence in shell script. For more content of multi command execution sequence of shell script, please search the previous articles of developeppaer or continue to browse the related articles below. I hope you can support developeppaer more in the future!