| Operators
usage:
&& operator:
format
command1 && command2
&& left after the command (command 1) returns true (ie returns 0, successfully executed), the right of && command (command 2) to be able to be executed; in other words, "If this command is successful && then execute this command." .
Syntax is as follows:
command1 && command2 && command3 ...
- Use && connection command, and implementing logical functions.
- Only && left of the command returns true (command returns the value of $? == 0) && right of the command will be executed.
- As long as there is a command returns false (command returns the value of $? == 1), the latter command will not be executed.
-
Example 1, cp command first home directory to copy files from the root to the next anaconda-ks.cfg / data directory; after successful execution, use the rm command to delete the source file; if successful, delete the output message "SUCCESS".
|| operator:
format
command1 || command2
&& and || the opposite. If || command on the left (command1) fails to execute, then execute commands on the right || (command2); or in other words, "If this command fails || then execute the command.
-
Use || connection command, or implementing logic functions.
-
Only the left || command returns false (command returns the value of $? == 1), the right of the || command will be executed. This logic and c language syntax or functionally identical, i.e., short-circuit or a logical operation.
-
As long as there is a command returns true (command returns the value of $? == 0), the latter command will not be executed.
-
In Example 2, dir if the directory does not exist, the message output fail.
-
Example 3, if the dir directory exists, the output success message; otherwise, the output fail message.
6. The following is a shell script commonly used combination example ||
echo $BASH |grep -q 'bash' || { exec bash "$0" "$@" || exit 1; } 系统调用exec是以新的进程去代替原来的进程,但进程的PID保持不变。因此,可以这样认为,exec系统调用并没有创建新的进程,只是替换了原来进程上下文的内容。原进程的代码段,数据段,堆栈段被新的进程所代替。
() Operator:
If you want to put together a few command execution, shell provides two methods. Both you can also execute a set of commands in a sub-shell in the current shell.
format:
(command1;command2;command3....) 多个命令之间用;分隔
- One requires exclusive physical line, if necessary multiple commands on the same line, using the command between the command separator (;) separated. Effect equivalent to the effect of executing a plurality of individual separate commands executed.
- () Indicates as a whole a plurality of commands executed in the current shell. Note that, using () enclosed command will not be executed on the front switch the current working directory, that is a combination of commands are to be executed in the current working directory, change directory command despite command.
- Often a combination of the command and executes the control command in combination.
-
In Example 4, dir if the directory does not exist, the command is executed in combination.
{} Operator:
If instead of using {} (), then the corresponding commands to be executed as a whole rather than in the sub-shell current shell, only the output of all commands in {} is redirected as a whole, which the command was only into sub-shell execution, or executed in the current shell.
Its general form:
{ command1;command2;command3… } 注意:在使用{}时,{}与命令之间必须使用一个空格
-
Example 5, except that {} is executed the printing operation in the sub-shell