the shell | && || () {} usage, and logical AND or shell

| Operators

Pipe symbol unix is a very powerful symbol as a vertical line: "|".
usage:
command 1 | command 2
His function is the result of executing the first command as an input command 1 command2 passed to command 2, for example:
 
$ Ls -s | sort -nr (be careful not to copy the $ symbol to go oh)

-s is the file size, -n is numeric-sort, -r is the reverse, reverse
The command lists the current directory document (including size), and outputs to the sort command as input, sort command descending order of the number of ls output is sorted.

&& 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 ...
  1. Use && connection command, and implementing logical functions.
  2. Only && left of the command returns true (command returns the value of $? == 0) && right of the command will be executed.
  3. As long as there is a command returns false (command returns the value of $? == 1), the latter command will not be executed.
  4. 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".


     
    Examples 1.jpg

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

  1. Use || connection command, or implementing logic functions.

  2. 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.

  3. As long as there is a command returns true (command returns the value of $? == 0), the latter command will not be executed.

  4. In Example 2, dir if the directory does not exist, the message output fail.


     
    Examples 2.jpg
  5. Example 3, if the dir directory exists, the output success message; otherwise, the output fail message.


     
    Examples 3.jpg

    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....)               多个命令之间用;分隔
  1. 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.
  2. () 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.
  3. Often a combination of the command and executes the control command in combination.
  4. In Example 4, dir if the directory does not exist, the command is executed in combination.


     
    Examples 4.jpg

{} 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… }      注意:在使用{}时,{}与命令之间必须使用一个空格
    1. Example 5, except that {} is executed the printing operation in the sub-shell


       

Guess you like

Origin www.cnblogs.com/huangguabushihaogua/p/12165467.html