$ if true;then echo YES; else echo NO; fiYES$ if false;then echo YES; else echo NO; fiNO
$ if true && true;then echo YES; else echo NO; fiYES$ if true && false;then echo YES; else echo NO; fiNO$ if false && false;then echo YES; else echo NO; fiNO$ if false && true ;then echo YES; else echo NO; fiNO
$ if true || true;then echo YES; else echo NO; fiYES$ if true || false;then echo YES; else echo NO; fiYES$ if false || true;then echo YES; else echo NO; fiYES$ if false || false;then echo YES; else echo NO; fiNO
$ if ! false;then echo YES; else echo NO; fiYES$ if ! true;then echo YES; else echo NO; fiNO
It can be seen
trueand
falseWe proceed according to our understanding of logical operations, but in order to better understand Shell's implementation of logical operations, we have to figure out,
trueand
falseHow does it work?
The answer is: No.
trueand
falseThey are not logical values themselves, they are all built-in commands of the Shell, but their return value is a "logical value":
$ true$ echo $?0$ false$ echo $?1
can be seen
truereturned 0, and
falseThen 1 is returned. It does not correspond to the true values 1 and 0 we learned in discrete mathematics, but is the opposite.
$ help true falsetrue: true Return a successful result.false: false Return an unsuccessful result.$ type true falsetrue is a shell builtinfalse is a shell builtin
illustrate:
$?It is a special variable that stores the end status (exit status code) of the last process.
From the above operations, it is not difficult to think of why emphasis is placed on C language programming.
mainAdd in front of the function
int, and add at the end
return 0. Because in the Shell, 0 will be used as a sign of whether the program ends successfully. This is what the Shell
trueand
falseIn essence, they are used to reflect whether a certain program ends correctly, rather than the traditional true and false values (1 and 0). On the contrary, they return 0 and 1. Fortunately, we don't need to worry about these when doing logical operations.
From the previous section, we have clearly understood what the "logical value" under Shell is: it is the return value when the process exits. If it returns successfully, it is true, if it returns unsuccessfully, it is false.
And the conditional test just uses
testSuch an instruction is used to perform numerical tests (various numerical attribute tests), string tests (various string attribute tests), and file tests (various file attribute tests). We judge whether the corresponding test is successful, thereby After completing various routine tasks, coupled with the logical combination of various tests, more complex tasks can be completed.
$ if test 5 -eq 5;then echo YES; else echo NO; fiYES$ if test 5 -ne 5;then echo YES; else echo NO; fiNO
$ if test -n not empty;then echo YES; else echo NO; fiYES$ if test -z not empty;then echo YES; else echo NO; fiNO$ if test -z ;then echo YES; else echo NO; fiYES$ if test -n ;then echo YES; else echo NO; fiNO
$ if test -f /boot/System.map; then echo YES; else echo NO; fiYES$ if test -d /boot/System.map; then echo YES; else echo NO; fiNO
$ a=5;b=4;c=6;$ if test $a -eq 5 -a $b -eq 4 -a $c -eq 6; then echo YES; else echo NO; fiYES
$ if test -f /etc/profile -o -d /etc/profile;then echo YES; else echo NO; fiYES
!NOT operation
$ if test ! -f /etc/profile; then echo YES; else echo NO; fiNO
The above only demonstrates
testCommand some very simple tests that you can pass
help testGet
testMore uses of . It should be noted that
testThere are some differences between the logical operations inside the command and the logical operators of the Shell. The corresponding ones are:
-aand
&&,
-oand
||, the two cannot be confused. Rather than operations being
!, compare them below.
$ cat > test.sh#!/bin/bashecho test[CTRL+D] # Press the key combination CTRL and D to end the cat input, the same as below, no longer specify $ chmod +x test.sh$ if test -s test .sh -a -x test.sh; then echo YES; else echo NO; fiYES$ if test -s test.sh && test -x test.sh; then echo YES; else echo NO; fiYES
$ str1=test$ str2=test$ if test -z $str2 -o $str2 == $str1; then echo YES; else echo NO; fiYES$ if test -z $str2 || test $str2 == $str1; then echo YES; else echo NO; fiYES
$ i=5$ if test ! $i -lt 5 -a $i -ne 6; then echo YES; else echo NO; fiYES$ if ! test $i -lt 5 -a $i -eq 6; then echo YES ; else echo NO; fiYES
It's easy to spot the difference between them,
-aand
-oAs a parameter of the test command, it is used inside the test command, and
&&and
||It is used to calculate the return value of the test,
!Common to both. What needs attention is:
Sometimes it is not necessary
!operator, such as
-eqand
-neJust the opposite, it can be used to test whether two values are equal;
-zand
-nIt is also corresponding and used to test whether a certain string is empty.
exist
Bashinside,
testCommands can be replaced with the [] operator, but it should be noted that [
Afterwards with] needs to be preceded by extra spaces
When testing a string, it is recommended that all variables be enclosed in double quotes to prevent the situation where there are only test parameters and no test content when the variable content is empty.
Below we use examples to demonstrate the above three precautions:
-neand
-eqCorrespondingly, we can sometimes dispense with
!Operation
$ i=5$ if test $i -eq 5; then echo YES; else echo NO; fiYES$ if test $i -ne 5; then echo YES; else echo NO; fiNO$ if test ! $i -eq 5; then echo YES; else echo NO; fiNO
use
[ ]can replace
test, it will look much more “beautiful”
$ if [ $i -eq 5 ]; then echo YES; else echo NO; fiYES$ if [ $i -gt 4 ] && [ $i -lt 6 ]; then echo YES; else echo NO; fiYES
Remember to add some string variables
,Remember[Afterwards with
]Add an extra space before
$ str=$ if [ $str = test]; then echo YES; else echo NO; fi-bash: [: missing `]'NO$ if [ $str = test ]; then echo YES; else echo NO; fi- bash: [: =: unary operator expectedNO$ if [ $str = test ]; then echo YES; else echo NO; fiNO
At this point, the conditional test has been introduced. The command list is introduced below. In fact, we have already used it above, that is, a combination of multiple test commands. Through
&&,
||and
!A sequence of combined commands. This sequence of commands effectively replaces
if/thenconditional branch structure. It is not difficult to think of the following multiple-choice questions we often do in C language programming (very boring example, but meaningful): Will the following print?
j, if printed, what will be printed?
#include <stdio.h>int main(){ int i, j; i=5;j=1; if ((i==5) && (j=5)) printf(%dn, j); return 0;}It is easy to know that the number 5 will be printed because
i==5This condition holds, and subsequently
&&, to judge whether the entire condition is established, we have to make a subsequent judgment, but this judgment is not a conventional judgment, but first
jModify it to 5 and then convert it to a true value, so the condition is true and 5 is printed. Therefore, this sentence can be interpreted as: If
iis equal to 5, then put
jAssign a value of 5 if
jis greater than 1 (because it was already true before), then print out
jvalue. Use it this way
&&The concatenated judgment statement replaces two
ifConditional branch statement.
It is precisely based on the unique properties of logical operations that we can pass
&&,
||to replace
if/thenWait for the conditional branch structure, thus generating a command list.
The execution rules of the command list conform to the operation rules of logical operations. Use
&&For connected commands, if the former returns successfully, the following command will be executed, otherwise; use
||If the connected commands return successfully, the subsequent commands will not be executed, and vice versa.
$ ping -c 1 www.lzu.edu.cn -W 1 && echo =======connected========
A very interesting question arises, which is what we have mentioned above: why should the C program be
main()Does the function return 0 at the end? If not, what would be the consequences of putting this kind of program into the command list? Write a simple C program yourself, and then put it into the command list to see.
Sometimes a command list is used instead
if/thenThe conditional branch structure can save some code and make the program more beautiful and easy to read, for example:
#!/bin/bashecho $#echo $1if [ $# -eq 1 ] && (echo $1 | grep ^[0-9]*$ >/dev/null);then echo YESfi
Note: The above example requires that the number of parameters is 1 and the type is numeric.
Plus
exit 1, we will omit
if/thenstructure
#!/bin/bashecho $#echo $1! ([ $# -eq 1 ] && (echo $1 | grep ^[0-9]*$ >/dev/null)) && exit 1echo YES
After this processing, the judgment of program parameters only requires a simple line of code, and it becomes more beautiful.
This section introduces logical operations, conditional testing and command lists in Shell programming.