Linux's shell

when executing a command shell, and returns a return value, the return value is stored in the shell variable $? in. ? When $ == 0, indicating success;? == time when $ 1, indicating execution failed.
Sometimes, the next command depend on whether the previous command executed successfully. Such as: re-executed after the successful execution of a command another command or another command is executed after the failure of a command and so on. && and || shell provided to implement the functions of the control command, to control the shell execute command according && behind or in front of the return value of || command.
&& (control command)
syntax is as follows:
Command1 && Command2 [Command3 && ...]
using 1 && connection command, and implementing logical functions.
2 && left in command only 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.
Example 1
malihou @ Ubuntu: ~ $ cp ~ / Desktop / 1.txt ~ / 1.txt && RM ~ / Desktop / 1.txt && echo "Success"
Command Examples 1 First copy from 1 ~ / Desktop directory. txt file to the directory ~; After the successful implementation, use rm to delete the source file; if the deletion is successful the system outputs.
|| (control command)
syntax is as follows:
Command1 || Command2 [Command3 || ...]
used || connected between a command, or implementing logic functions.
2 || left in command only 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 returns true as long as there is a command (command returns the value of $? == 0), the latter command will not be executed.
Example 2
malihou Ubuntu @: ~ $ ~ RM / Desktop / 1.txt || echo "Fail"
In Example 2, if ~ / Desktop directory does not exist under the file 1.txt, the output message.
Example 3
malihou Ubuntu @: ~ $ ~ RM / Desktop / 1.txt && echo "success" || echo "Fail"
In Example 3, if a file 1.txt ~ / directory under the Desktop, the output success message; otherwise, output fail message.

shell provides two methods ((), and {}) achieve Cooperating with few commands, instead performed independently. This approach does not need to execute the control command, the only commands together a plurality of individual execution, the command will return a value of the final return value of the last command is determined.
() (A combination of the command)
syntax is as follows:
(Command1; Command2 [; Command3 ...])
. 1 a requires exclusive physical line, if necessary multiple commands on the same line, between a command using the command separator ( ;) delimited. Effect equivalent to the effect of executing a plurality of individual separate commands executed.
2 () denotes 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 and often a combination of the command execution control command used in combination.
Example 4
malihou Ubuntu @: ~ $ ~ RM / Desktop / 1.txt || (CD ~ / Desktop /; -a LS; echo "Fail")
In Example 4, if the directory ~ / Desktop the absence of a document. txt, then execute the command combination.


See Reference URL: http: //blog.csdn.net/xyls12345/article/details/39054555

Guess you like

Origin www.cnblogs.com/lipengsheng-javaweb/p/11441715.html