当前位置:网站首页>Shell process control - 35. Multi branch case conditional statements

Shell process control - 35. Multi branch case conditional statements

2022-06-21 11:24:00 Flourishing

1、case Introduction to conditional statements

case Statement and if…elif…else Statements are also multi branch conditional statements , But and if The difference between multi branch conditional statements is that ,case Statement can only judge one kind of conditional relation , and if Statement can judge a variety of conditional relations .

case The sentence syntax is as follows :

case $ Variable name  in
    " value 1")
         If the value of the variable is equal to the value 1, Then execute the procedure 1
        ;;
    " value 2")
         If the value of the variable is equal to the value 2, Then execute the procedure 2
        ;;

    … Omit other branches …

    *)
         If none of the values of the variables are above , Then execute this procedure 
        ;;
esac  ( notes :case Reverse writing of )

explain :

case Statement and if…elif…else The difference between sentences :

case Only one condition can be judged in the statement ( value ), Whether it conforms to a certain situation ( value ), It's a judgment .

and if…elif…else sentence , According to the example in the previous article , We can see if Multi branch conditional statement , You can determine whether an input is empty , Is it a document , Whether it is a directory or not , Is a different judgment .

  • case sentence , It takes out the value in the variable , Then compare with the values in the body of the statement one by one .
    If the values match , Then execute the corresponding program , If the values don't match , Then compare the next value in turn .
    If all the values don't match , execute *) The next procedure ,*) Represents all other values .
  • case Statement to “case” start , With “esac” ending .
  • Each branch program is followed by ;; Double semicolon end , Represents the end of the segment .

3、 practice

Example 1

#!/bin/bash

#  Please enter the city you want to go to 
echo "want to beijing,please input 1."
echo "want to shanghai,please input 2."
echo "want to chendu,please input 3."

#  Receive input information , Assign a value to a variable cho
read -t 30 -p "please input your choice: " cho

# case conditional 
case $cho in
    "1")
    echo "to beijin"
        ;;
    "2")
        echo "to shanghai"
        ;;
    "3")
        echo "to chendu"
        ;;
    *)
        echo "error input"
        ;;
esac

Example 2

Put the last article if In the computer practice of multi branch conditional statements , The third level if Nesting changed to case Statements for .

#!/bin/bash
#  Character interface addition, subtraction, multiplication and division calculator .

# 1. adopt read The command receives the value to be calculated , And give variables num1 and num2
read -t 30 -p "Please input num1: " num1
read -t 30 -p "Please input num2: " num2

# 2. adopt read The command receives the symbol to be calculated , And give variables ope.
read -t 30 -p "Please input a operator: " ope

#  The first level of judgment 
#  Used to determine num1、num2 and ope It's worth it .
# -n Options : Judge whether the following string is empty , Not empty is true .
# -a Options : Logic and .
if [ -n "$num1" -a -n "$num2" -a -n "$ope" ]
    then
        #  Judge the input num1 and num2 Whether the content is purely digital  
        #  There are many ways to determine whether the contents of a variable are pure numbers , The following one is simple and easy to understand .
        #  Defining variables test1 and test2 The value of is $( command ) Result .
        test1=$(echo $num1 | sed 's/[0-9]//g')
        test2=$(echo $num2 | sed 's/[0-9]//g')

        #  explain :
        #  It is through sed command , Put variables num1 Every character in the value , As long as it is 0-9 The content of , Replace with empty .
        #  Finally, if test1 The last value of is null , prove num1 The contents of variables are all numbers , Because they were all replaced .
        #  If test1 The last value of is not empty , prove num1 The contents of the variable are non numeric , namely num1 Impure number .

        #  The second level of judgment , Used to determine num1 and num2 Is the value .
        #  If the variable test1 and test2 The value of is empty , Then prove numl and num2 It's the number. .
        # -z Options : Determines if the string is empty ( Return true for null )
        # -a Options : Logic and .
        if [ -z "$test1" -a -z "$test2" ]
            then
                #  If test1 and test2 It's the number. , Then execute the following command .

                #  The third level of judgment is used to confirm the operator .
                #  Confirm variables $ope What operator is in .
                case $ope in
                    "+")
                        #  If it is a plus sign, the addition operation is performed .
                        value=$(($num1+$num2))
                        ;;
                    "-")
                        #  If it's a minus sign , The subtraction operation is performed .
                        value=$(($num1-$num2))
                        ;;
                    "*")
                        #  If it's a multiplication sign , Then perform multiplication .
                        value=$(($num1*$num2))
                        ;;
                    "/")
                        #  If it's a division sign , Then perform the division operation .
                        value=$(($num1/$num2))
                        ;;

                    *)
                        #  If the operators don't match , Prompt for a valid symbol 
                        echo "Please enter a valid symbol."
                        #  And quit the program , Return error code 10.
                        exit 10
                        ;;
                esac
        else
            #  If test1 and test2 Not empty , explain numl and num2 Not numbers .
            #  You will be prompted to enter a valid value .
            echo "Please enter a valid value."
            #  And quit the program , Return error code 11.
            exit 11
        fi

else
    echo "qing input neirong."
    exit 12
fi

#  Output the result of numerical operation .
echo "$num1 $ope $num2 :$value"

原网站

版权声明
本文为[Flourishing]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206211103061534.html