preface

Shell can also use if-else, switch-case and other branch statements similar to other languages. Although there are some differences in syntax with other languages, the semantics are similar. The following is an example of how to write control statements.

Control statement writing example

If statement

There is only one branch: if-then

Grammar:

If is true; then

Perform related operations

fi

P.S. : then if you don’t rewrite a line, remember to add the semicolon (;) before it.

Example:

#! /bin/bash

if [ 2 -gt 1 ];then
        echo 2 > "1"
fi
Copy the code

Two branches: if-then-else

Writing:

If expression is true; then

Execute Expression 1

else

Execute expression 2

fi

Example:

if [ 2 -lt 1 ];then
        echo "2" 1"
else
        echo 2 > "1"
fi
Copy the code

Multiple branches: if-then-elif-… -else

Writing:

If expression 1 is true; then

Perform Operation 1

Elif expression 2 is true; then

Perform Operation 2

Elif expression 3 is true; then

Perform Operation 3.

. There can be multiple ElIfs

else

If none of the preceding conditions are met, perform this operation

fi

You can also omit the last else, so if none of these conditions are met, then none of them are executed.

Example:

#! /bin/bash

number=The $1
if [ "x$number" = "x" ];then
        echo "number is null"
elif [ $number -eq 1 ];then
        echo "number is 1"
elif [ $number -eq 2 ];then
        echo "number is 2"
elif [ $number -eq 3 ];then
        echo "number is 3"
else
        echo "number is unknown"
fi
Copy the code

In the example, $1 is a positional argument, and you need to understand the use of special variables that you can see in the bash shell’s basic syntax

A case statement

Use the case syntax, which is semantically similar to C or Java’s Switch case, but feels more powerful. Similarly, its matching condition is not an expression, but matches whether the values are equal (exists).

Writing:

In case variables

Matching condition 1)

Perform operation 1.

Matching condition 2)

Perform operation 2.

    *)

Do not match the operation performed;

esac

Note that each match condition is followed by one), and that each code executed is followed by two semicolons, similar to the C/Java break. Of course, in bash’s help documentation, the semicolons “;;” Can be replaced with “; & “or”;; & “, has other functions, as described in the following document:

If the ;; operator is used, no subsequent matches are attempted after the first pattern match. Using ; & in place of ;; causes execution to continue with the list associated with the next set of patterns. Using ;; & in place of ;; Causes the shell to test the next pattern list in the state‐ ment, if any, and execute any associated list on a successful match.

If you are interested, try the last *), which is executed when none of the previous conditions match (default).

Now write a number of ways to write this matching condition (not all).

#! /bin/bash

case The $1 in
        1)
                echo "input is 1";;
        2)
                echo "input is 2";;
        'a')
                echo "input is a";;
        'b'|'c')
                echo "input is b|c";;
        [A-Z])
                echo "input is [A-Z]";;
        *)
                echo "unknown input";;

esac
Copy the code

Execution Result:

P.S. : The matching condition can be one or more strings, not just one character. In the example, it is all one character.

In fact, case+ Shift is a good way to parse parameter options, and I’ll write an example in the next post if I write shift.