Linux Shell && | | --Reprint

Source: Internet
Author: User

Linux shell && | |  shell returns a return value when executing a command that is saved in the shell variable $? In When $? = = 0 o'clock, indicating successful execution; = = 1 o'clock, which indicates that the execution failed.    www.2cto.com   Sometimes the next command depends on whether the previous command succeeded. For example, execute another command after executing a command successfully, or execute another command after a single command fails. The shell provides && | | To implement command execution control, the shell will be based on && or | | The return value of the preceding command to control the execution of subsequent commands.   && (Command execution Control)    syntax format is as follows:      Command1 && command2 [&& Command3 ...] The   1 command uses a && connection to implement logic and functionality.  2 only the command on the left of && returns True (command return value $?). = = 0) The command to the right of,&& will be executed.  3 as long as there is a command return False (command return value $?) = = 1), the subsequent command will not be executed.    www.2cto.com   Example 1 [email protected]:~$ CP ~/desktop/1.txt ~/1.txt && RM ~/desktop/ 1.txt && echo "Success"    the command in Example 1 first copies the 1.txt file from the ~/desktop directory to the ~ directory; After successful execution, the source file is deleted using RM, and if the deletion succeeds, the message is output.   | | (Command execution Control)    syntax format as follows:      Command1 | | Command2 [| | Command3 ...] Use between   1 commands | | A connection that implements logic or functionality.  2 Only in | | The left command returns False (command returnreturn value $? = = 1), | | The command on the right will not be executed. This is the same as the logical or syntactic function in C, which is to implement short-circuit logic or operations.  3 as long as there is a command return True (command return value $?) = = 0), the subsequent command will not be executed.    Example 2 [email protected]:~$ RM ~/desktop/1.txt | | echo "Fail"    in Example 2, if file 1.txt does not exist under the ~/desktop directory, the prompt is output.    Example 3 [email protected]:~$ rm ~/desktop/1.txt && echo "Success" | | echo "Fail"    in Example 3, if file 1.txt is present in the ~/desktop directory, the success prompt is output, otherwise the fail prompt message is output.   shell provides two methods (() and {}) implementations to execute together in conjunction with several commands, instead of performing independently. This method does not control whether the command needs to be executed, only the combination of multiple individual commands, and the return value of the final command is determined by the return value of the last command.    () (command combination)    syntax format is as follows:   www.2cto.com       (command1;command2[;command3 ...])   1 a command needs to have a single physical line, and if multiple commands need to be placed on the same line, the command delimiter (;) is used to separate the commands. The effect is equal to the effect that multiple independent commands perform separately.  2 () indicates that multiple commands are executed as a whole in the current shell. It is important to note that commands that are enclosed in () do not switch the current working directory before execution, which means that the command combination is executed in the current working directory, although there are commands to switch directories in the command. The  3 command combination is often used in conjunction with command execution control.    Example 4 [email protected]:~$ RM ~/desktop/1.txt | | (CD ~/desktop/;ls-a;echo "fail")    in the example4, if file 1.txt does not exist under directory ~/desktop, a combination of commands is executed.   Transferred from: http://www.2cto.com/os/201302/189655.html
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.