Linux shell flow control (condition if, loop [for, while], select [case] statement instance

  
 

The linux shell has its own set of flow control statements, including conditional statements (if), loop statements (for, while), and select statements (case). Below I will introduce the use of each statement by example.


First, shell conditional statements (if usage)

if statement structure [if/then/elif/else/fi]


if conditional test statement

then

action

[elif condition

action

else

Action

]

fi


If it is not clear about the conditional test statement, you can refer to: linux shell logical operators, logical expressions

The shell command can be split by semicolon or by line break. If you want to write multiple commands in one line, you can split them by “';”.

如:

[chengmo@centos5 ~]$ a=5;if [[ a -gt 4 ]] ;then echo 'ok';fi; ok


Instance: (test.sh)

#!/bin/sh

scores=40;if [[ $scores -gt 90 ]]; then echo " Very good!";elif [[ $scores -gt 80 ]]; then echo "good!";elif [[ $scores -gt 60 ]]; then echo "pass!";else echo " No pass!";fi;



The conditional tests are: [[],[],test These, note: [[]] Variables are separated by spaces.


Second, loop statement (for, while, until use):

  • for loop method (for/do/done)

    Syntax structure:

    1.for … in statement

    for variable in seq string

    do

    action

    done

    Description: The seq string is separated by a space character. Each time for…in is read, the value will be read in order, giving the previous variable.

    Instances (testfor.sh):

    #!/bin/sh

    for i in $(seq 10); do echo $i;done;


    seq 10 produces 1 2 3 . . . . 10 space separated strings.

    2.for((assignment; condition; operation statement))

    for((assignment; condition; operation statement))

    do

    action

    done;

    Instance (testfor2.sh):

    #!/bin/sh

    for((i=1;i<=10 ;i++));do echo $i;done;





  • while loop (while/do/done)< Br>

    while statement structure

    while conditional statement

    do

    action

    done;

    Instance 1 :

    #!/bin/shi=10;while [[ $i -gt 5 ]];do echo $i; ((i--));done;

    Run results :================================================================================= :)

    #!/bin/sh

    while read line;do echo $line;done < /etc/hosts;


    Run Result: ===================

    sh testwhile2.sh

    # Do not remove the following line, or various programs# that Require network functionality will fail.127.0.0.1 centos5 localhost.localdomain localhost

  • until loop statement

    syntax structure:

    until condition

    do

    action

    done

    means: Exit until the condition is met. Otherwise execute action.

    Instance (testuntil.sh):

    #!/bin/sh

    a=10;

    until [[ $a -lt 0 ]];do

    echo $a;

    ((a—));

    done;

    Results:

    sh testuntil.sh

    109876543210

    Three, shell selection statement (case, select usage)

  • case selection statement use (case/esac)

    Syntax Structure

    case $arg in pattern

  • Copyright © Windows knowledge All Rights Reserved