Shell Programming-Conditional Judgment

Conditional Judgment of Shell Programming

Conditional test operation

test command

Test whether a particular expression is valid. When the condition is valid, the return value of the test statement is 0, otherwise it is another value.

Format 1: test conditional expression
 Format 2: [Conditional expression]

Document testing
[Operator file or directory]
Common test operators
- d: Test if it is a directory
- e: Testing whether a directory or file exists
- f: Test whether it is a file or not
- r: Test whether the current user has permission to read
- w: Test whether the current user has permission to write
- x: Test whether the current user has permission to execute

Integer numerical comparison
[Integer 1 operator integer 2]
Common test operators
- eq: equal to
- ne: Not equal to
- gt: greater than
- lt:less than
- le: less than or equal to
- ge: greater than or equal to

string comparison
Format 1: [String 1 = String 2]
[String 1! = String 2]
Format 2: [- z string]

Logic Testing
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 |: Logic or "or" means
!: Logical No

The Structure of If Statement

Single branching structure

if                          //Conditional test operation
    then                  //Command Sequence
fi                        //End Command Sequence Judgment

Examples of scripts:
#!/bin/bash
#Compare input integers to input integers
read -p "Please enter an integer:" num
if [ $num -ge 5 ]
 then
        echo "That's big."
fi

Bifurcation structure

if                               //Conditional test operation
    then                      //Command Sequence 1
    else                    //Command Sequence 2
fi                         //End Command Sequence Judgment

Examples of scripts:
#!/bin/bash
#Compare input integers to input integers
read -p "Please enter an integer:" num
if [ $num -ge 5 ]
 then
        echo "That's big."
 else
        echo "That's small."
fi

Multi-branch structure

if                             //Conditional test operation 1
    then                      //Command Sequence 1
elif                          //Conditional test operation 2
    then                     //Command Sequence 2else            
                               //Command Sequence 3
fi                            //End Command Sequence Judgment

#!/bin/bash
read -p "Please enter your score:" num
if [ $num -gt 85 ] && [ $num -le 100 ]
 then
        echo "excellent"
elif [ $num -gt 60 ] && [ $num -le 85 ]
 then
        echo "good"
 else
        echo "Fail,"
fi

Nested if statement

#!/bin/bash
read -p "Please enter your match time:" num
if [ $num -lt 10 ]
 then
        echo "Congratulations on your entering the finals"
 read -p "Your gender is:" sex
 if [ $sex = "male" ]
  then
        echo "You enter the men's group"
 else
        echo "You enter the women's group"
 fi
else
        echo "I'm sorry you missed the finals."
fi

Thank you for reading!!!

Tags: Linux less shell Programming

Posted on Thu, 10 Oct 2019 02:57:36 -0700 by swissbeets