In the shell, the command execution succeeds with a return value of 0, and the unsuccessful return value is not 0
Logic with:
Shell 0 is true, not 0 is false
When the first condition is true, the second condition must be judged;
When the first condition is false, the second condition ceases to be judged and the final result is false;
When the first command executes successfully continues with the second command, the final result is determined by the result of the second command execution.
When the first command fails, the second command is no longer executed, and the end result is a failed one.
Understood as multiplication (0 is false 1 is true)
1*1=1 1*0=0 0*1=0 0*0=0
Cases:
1, User1 exist, the previous command executed successfully, the second command executed successfully, the final result for success (1*1=1)
[[email protected] shell]# ID user1uid=501 (user1) gid=501 (user1) groups=501 (user1) [[email protected] shell]# echo $?0[[ Email protected] shell]# ID user1 &>/dev/null && echo Hello user1hello user1[[email protected] shell]# Ech o $?0
2, User1 does not exist, the previous command failed to execute, the latter command is no longer executed, the end result is a failure (0*?=0)
[[email protected] shell]# ID user1id:user1:No such user[[email protected] shell]# echo $?1[[email protected] shell]# ID User1 &>/dev/null && Echo Hello user1[[email protected] shell]# echo $?1
3, User1 exists User2 does not exist the previous command executed successfully, the latter command failed to execute, the command final execution failed (1*0=0)
[[email protected] shell]# ID user1uid=501 (user1) gid=501 (user1) groups=501 (user1) [[email protected] shell]# echo $?0[[ Email protected] shell]# ID user2id:user2:No such user[[email protected] shell]# echo $?1[[email protected] shell]# ID u Ser1 &>/dev/null && ID user2id:user2:No such user[[email protected] shell]# echo $?1
Logical OR:
Shell 0 is true, not 0 is false
When the first condition is true, the second condition ceases to be judged and the final result is true;
When the first condition is false, the second condition must be judged.
When the first command executes successfully the second command is no longer executed, and the final result is successful.
When the first command fails to continue executing the second command, the final result is determined by the execution result of the second command.
Understood as an addition operation (0 is false 1 is true)
0+0=0 1+0=1 1+1=1 0+1=1
1, User1 existence of the previous command succeeds after one no longer executes the final result for success (1+? =1)
[[email protected] shell]# ID user1uid=501 (user1) gid=501 (user1) groups=501 (user1) [[email protected] shell]# echo $?0[[ Email protected] shell]# ID user1 &>/dev/null | | Useradd user1[[email protected] shell]# echo $?0
2, User1 does not exist the previous command failed to execute the second command successfully succeeded in the final result (0+1=1)
[[email protected] shell]# ID user1id:user1:No such user[[email protected] shell]# echo $?1[[email protected] shell]# ID User1 &>/dev/null | | Useradd user1[[email protected] shell]# echo $?0[[email protected] shell]# ID user1uid=501 (user1) gid=501 (user1) groups=5 (user1)
3, User1 does not exist the previous command failed to execute; User2 does not exist after the execution of a command fails; the end result fails (0+0=0)
[[email protected] shell]# ID user1id:user1:No such user[[email protected] shell]# echo $?1[[email protected] shell]# ID User2id:user2:No such user[[email protected] shell]# echo $?1[[email protected] shell]# ID user1 &>/dev/null | | ID user2 &>/dev/null[[email protected] shell]# echo $?1
Execution results can be used! To take the reverse to make the real change false, false change true
[[email protected] shell]# ID user1id:user1:No such user[[email protected] shell]# echo $?1[[email protected] shell]#! ID user1id:user1:No such user[[email protected] shell]# echo $?0[[email protected] shell]# ID user1uid=501 (user1) gid=50 1 (User1) groups=501 (user1) [[email protected] shell]# echo $?0[[email protected] shell]#! ID user1uid=501 (user1) gid=501 (user1) groups=501 (user1) [[email protected] shell]# echo $?1[[email protected] shell]#! ID user1 &>/dev/null && useradd user1[[email protected] shell]# echo $?1
Composite instances
If User1 does not exist, add user1 if there is output Hello User1
[[email protected] shell]# ID user1id:user1:No such user[[email protected] shell]# ID user1 &>/dev/null && ; echo "Hello user1" | | Useradd user1[[email protected] shell]# echo $?0[[email protected] shell]# ID user1uid=501 (user1) gid=501 (user1) groups=5 (user1) [[email protected] shell]# ID user1 &>/dev/null && echo "Hello user1" | | Useradd User1hello User1
This article is from the "Greg's bustling" blog, please be sure to keep this source http://gregdefanhua.blog.51cto.com/7580626/1563920
Logical relationships between commands in the shell