Conditional test of shell script and usage of if conditional statement

Time:2020-10-18

1、 Conditional test operation

To make a shell script program have a certain “intelligence”, the first problem is how to distinguish different situations to determine what operation to perform. The shell environment returns the status value (¥?) after the command is executed When the return value is 0, it means success, otherwise (non-0 value) indicates failure or exception. Using the special test tool — test command, you can test a specific condition and judge whether the condition is true according to the return value (a return value of 0 means that the condition is true).

When using the test command, there are two forms:

Test conditional expression
[conditional expression]

The two methods work exactly the same, but the latter is usually more commonly used. It should be noted that at least one space is required to separate the square bracket “[” or “]” and the conditional expression.

1. File test:

-d: Test whether it is a directory;

-e: Test whether the file or directory exists (exist);

-f: Test whether it is a file or not;

-r: Test whether the current user has read permission;

-w: Test whether the current user has permission to write;

-x: Test whether the executable permission is set;

After the conditional test operation is executed, the pre-defined variable $? The return status value of the test command can be obtained to determine whether the condition is true. For example, you can test the existence of the directory / media / if the return value is $? If it is 0, it means that this directory exists; otherwise, it means that it does not exist or exists but is not a directory.

[ [email protected]  ~]#Test - D / boot <! -- test whether the directory exists -- >
[ [email protected]  ~]#Echo $? <! -- view the return value of the previous command -- >
0 <! -- return 0 to indicate the condition is true -- >

If the condition of the test is not true, the return value of the test operation will not be 0 (usually 1)

[ [email protected]  ~]#Test - D / ertec <! -- test whether the directory exists -- >
[ [email protected]  ~]#Echo $? <! -- view the return value of the previous command -- >
1 <! -- returns 1 to indicate that the condition is not true -- >

2. Comparison of integer values:

-EQ: the first number is equal to the second number;

-NE: the first number is not equal to the second number;

-GT: the first number is greater than the second number;

-LT: the first number is less than the second number;

-Le: the first number is less than or equal to the second number;

-Ge: the first number is greater than or equal to the second number;
Integer value comparison is widely used in Shell Scripting. For example, it is used to determine the number of logged in users, the number of open processes, whether the disk utilization rate exceeds the standard, and whether the software version number is required by symbols. In practice, we often get a value by variable reference, command replacement and so on.

3. String comparison:

=: the first string is the same as the second string.

! =: the first string is not the same as the second string, where “!” Indicates negation.

-z: Check whether the string is empty. For variables that are not defined or given a null value, it will be an empty string.

[[email protected] ~]# a=1
[[email protected] ~]# b=2 
[ [email protected]  ~]#[$a = $b] & & echo "yes" <! -- when 1 is equal to 2, yes is displayed, not null -- >
      <! -- display empty -- >
[[email protected] ~]# a=1 
[[email protected] ~]# b=2 
[ [email protected]  ~]#[$a! = $b] & & echo "yes" <! -- when 1 is not equal to 2, yes is displayed, otherwise it is empty -- >
Yes <! -- 1 is not equal to 2, yes -- > is displayed

4. Logic test:

  • &&: logical and means “and”. Only when the last two conditions are true, the return value of the entire test command is 0 (the result is true). When using the test command to test, “& &” can be replaced by “- a”.
  • ||: logical or means “or”. As long as one of the two conditions is true, the value returned by the whole test command is 0 (the result is true). You can use “- O” instead of using the test command to test.
  • ! : logical no means “no”. Only when the condition is not true, the value returned by the whole test command is 0 (the result is true).

Examples are as follows:

[[email protected] ~]# a=100
[ [email protected]  ~]#[$a = 100] & & [$a! = 50] & & echo "yes" <! -- use logical and operation, two conditions are true, show yes -- >
Yes <! -- two conditions are true and yes -- > is displayed
[[email protected] ~]# c=100 
[ [email protected]  ~]#[$a = 100] | [$a - Ge 50] & & echo "yes" <! -- using logical and operations, one of the two conditions is true and displays as yes -- >
Yes <! -- two conditions, one of which is true, shows yes -- >

2、 Using if conditional statements

1. Single branch if statement

The “branch” of an IF statement refers to one or more execution statements corresponding to different test results. For a single branch selection structure, the corresponding code is executed only when the condition is true, otherwise no operation is performed. The syntax format of a single branch if statement is as follows:

Example of single branch if statement:

[ [email protected]  ~]#CD / usr / SRC / PPP <! -- verify that the directory exists -- >
-Bash: CD: / usr / SRC / PPP: there is no such file or directory <! -- the PPP directory does not exist -- >
[ [email protected]  ~]# vim if_ dan.sh  <! -- script to create the / usr / SRC / PPP directory -- >
#! / bin / bash <! -- script statement -- >
Mount = / usr / SRC / PPP "<! -- define a variable -- >
If [! - D $mount] <! -- Test Condition -- >
Then <! -- if satisfied, execute the following command -- >
    mkdir -p $mount       
Fi <! -- end of if statement -- >
[ [email protected]  ~]# chmod +x if_ dan.sh     Add permission
[ [email protected]  ~]# ./if_ dan.sh      <! -- execute script automatic creation -- >
[ [email protected]  ~]#CD / usr / SRC / PPP / <! -- check whether the directory is created successfully -- >
[[email protected] ppp]# ls

2. Double branch if statement

For the double branch selection structure, different operations are required for the two cases of “condition is true” and “condition is not true”. The syntax format of a double branched if statement is as follows:

Example of double branch if statement:

[ [email protected]  ~]# vim if_ shuang.sh  <! -- write scripts to test network connectivity -- >
#!/bin/bash
Ping - C 3 - I 0.2 - w 3 $1 - > / dev / null <! -- Ping 3 times, interval 0.2 seconds, timeout 3 seconds, and $1 is the location variable -- >
If [$? - EQ 0] <! -- if the test condition is that the previous command is executed successfully -- >
Then <! -- if successful, execute the following command -- >
  echo "Host:$1 is UP!!!"
Else <! -- if not, execute the following command -- >
  echo "Host:$1 is DOWN!!!"
Fi <! -- end of if statement -- >
[ [email protected]  ~]# chmod +x if_ shuang.sh     <! -- add execution permission -- >
[ [email protected]  ~]# ./if_ shuang.sh  192.168.100.10 <! -- use script to test whether 100.10 network is communicating -- >
Host:192.168.100.10 is  Up
[ [email protected]  ~]# ./if_ shuang.sh  192.168.100.103 <! -- use script to test whether 100.103 network is communicating -- >
Host:192.168.100.103 is  Down

3. Multi branch if statement

Because if statements can execute operations according to whether the test results are true or not, all if statements can be nested and judged multiple times. For example, first judge whether a student’s score is qualified, and if so, judge again whether the score is higher than 90. The syntax format of a multi branch if statement is as follows:

Example of multi branch if statement:

[ [email protected]  ~]# vim if_ duo.sh   <! -- write scripts to test students' scores -- >
#!/bin/bash
Please input the test result: "insert" 
If [$insert - Ge 85] & - [$insert - Le 100] <! -- 85 ~ 100 points, excellent -- >
  then
    Echo "congratulations on your excellent test results!!! "
Elif [$insert - Ge 70] & - [$insert - Le 84] <! -- 70 ~ 84 points, qualified -- >
  then
    Echo "congratulations on passing the exam!!! "
Else <! -- other scores, unqualified -- >
    "I'm sorry that you can pack up your test results and go home to plant corn!"!!! "
Fi <! -- end of if statement -- >
[ [email protected]  ~]# chmod +x if_ duo.sh    <! -- add execution permission -- >
[ [email protected]  ~]# ./if_ duo.sh   <! -- run script -- >
Please enter the test result: 94 <! -- 94 excellent -- >
Congratulations on your excellent test results!!!
[[email protected] ~]# ./if_duo.sh
Please input test result: 82
Congratulations on passing the exam!!!
[[email protected] ~]# ./if_duo.sh 
Please input test score: 71
Congratulations on passing the exam!!!
[[email protected] ~]# ./if_duo.sh 
Please input test result: 32
It's a pity that you can pack up your exam results and go home to plant corn!!!

The above is the whole content of this article, I hope to help you in your study, and I hope you can support developeppaer more.