Conditional statements for shell programming

Keywords: Linux

catalogue

preface

1, Condition test

1.test command

2. Document test

  3. Integer value comparison  

4. String comparison

5. Logic test

2: if conditional statement

1. Single branch structure

2. Double branch structure  

3. Multi branch structure  

3, case multi branch structure

summary

preface

There are many conditional judgment methods. Today, let's learn about the relevant commands of test

1, Condition test

1.test command

Test whether the expression is true. If it is true, return 0; otherwise, return other values

Format 1: test Conditional expression

Format 2:[ Conditional expression ]

Note that you need a space between the brackets and the expression

2. Document test

Format:[ Operator file or directory ]

File testing refers to judging whether the corresponding file or directory is based on the given path name, or whether the file is readable, writable, executable, etc. The common operation options for file testing are as follows. When using, you can put the test object after the operation options.

Common test operators

-d:Test whether it is a directory(Directory) . 

-e:Test whether the directory or file exists(Exist). 

-f:Test whether it is a file(File) . 

-r:Test whether the current user has permission to read(Read). 

-w:Test whether the current user has permission to write(Write) . 

-x:Test whether an executable is set(Excute) jurisdiction.

-b:Test whether it is a device file

-c:Test whether it is a character device file

-s:The test exists and the file size is empty

-L:Test for linked files

After the conditional test operation is executed, through the predefined variable $? The return status value of the test command can be obtained to judge whether the condition is true. For example, do the following to test whether the directory / media / exists. If the return value is $? 0 indicates that this directory exists, otherwise it does not exist or exists but is not a directory

  Generally, we use [] more and choose one of the two methods according to our personal preferences

  3. Integer value comparison  

Format:[ Integer 1 operator integer 2]

Common test operators

-eq     The two values are equal (equal)  
-ne     The two values are unequal (not equal)  
-gt     n1 greater than n2 (greater than)  
-lt     n1 less than n2 (less than)  
-ge     n1 Greater than or equal to n2 (greater than or equal)  
-le     n1 Less than or equal to n2 (less than or equal) 

4. String comparison

Format 1: [ String 1=String 2 ]
       [ String 1!=String 2]

Format 2:[ -z character string ]


Common test operators

=:The string content is the same

!=:Different string contents,!The sign means the opposite

-z:The string content is empty

test -z string determines whether the string is 0? true if string is an empty string   test -n string determines whether the string is non-zero? false if string is an empty string.  
Note: - n can also be omitted  
test str1 = str2 determines whether str1 is equal to str2. If it is equal, it returns true  
test str1 != str2 determines whether STR1 is not equal to str2. If it is equal, false is returned  

5. Logic test

Format 1:[ Expression 1 ] Operator [ Expression 2 ]

Format 2: Command 1 operator command 2

Common test operators

-a or&&Logic and the meaning of "and"
-o or||Logical or meaning of "or"
!Logical no

 - a   (and) the two conditions are established at the same time! For example, if test -r file -a -x file, true will be returned only when the file has both r and X permissions.  

-o (or) either of the two conditions holds! For example, if test -r file -o -x file is used, true can be returned when the file has r or X permissions.  
! Reverse phase status, such as test- x file. When the file does not have x, it returns true  
 

a=5
[ $a -ne 1 ] && [ $a != 2 ] Equivalent to [ $a -ne 1 -a $a != 2 ]
[ -d 11 ] && [ -f 2.txt ] && echo "yes"
[ -d 11 -a -f 2.txt ] && echo "yes"
[[ -d 11 && -f 2.txt ]] && echo "yes"
 
a=6
[ $a -ne 5 ] && [ $a -gt 5 ] && echo "yes"
[ $a -ne 5 -a $a -gt 5] && echo "yes"
[[ $a -ne 5 && $a -gt 5 ]] $$ echo "yes"
 
[[ $a -ne 5  || $a -gt 5 ]] && echo "yes"
[$a -ne 5 ] || [ $a -gt 5 ] && echo "yes" 

&&,||Operators can exist normally[[ ]]Conditional judgment structure, but if it appears in a[ ]In the structure, an error will be reported 

2: if conditional statement

1. Single branch structure

if     Conditional test operation                      if Used disk>80%
then Command sequence                            then call the police   
fi  									fi

 

 

2. Double branch structure  

Judge whether the target host is alive and display the test results

if Conditional test operation                   if 80 Is the port listening
   then                            then  
   Command sequence 1                     The site service is already running  
   else                            else
   Command sequence 2                       start-up httpd service
fi                              fi

3. Multi branch structure  

if Conditional test operation                if Score 85-100 between
   then                         then 
   Command sequence 1                      Judged excellent
elif                         elif
 Condition test operation 2                The score is 70-84 between
   then                          then
   Command sequence 2                       Qualified
else                          else
 Command sequence 3                      Disqualification
fi                            fi

3, case multi branch structure

case Variable value in 
Mode I)
   Command sequence
;;
Mode II)
   Command sequence 
;;
......
*)
   Default command sequence
esac

be careful!

(1) the first mock exam must be case, and each mode must be ended with a single right bracket ("in").

② The double semicolon ";" indicates the end of the command sequence

③ In the pattern string, a continuous range can be represented by square brackets, such as "[0-9]"; You can also use the vertical bar | to represent or, such as a|b

The last *) represents the default mode, where * is equivalent to a wildcard

summary

Today, we first understand the format of the script, and then understand the principle

Come on, every day is bright

Posted by notepad on Fri, 01 Oct 2021 12:03:15 -0700