Shell script from entry to complex seven (loop)

One, if loop

The syntax format is as follows:


Single branch if statement:


if condition

then

    command1 

    command2

    ...

    commandN 

be


Double branch if statement:


if condition

then

    command1 

    command2

    ...

    commandN

else

    command

be

 


Multi-branch if statement:


if condition1

then

    command1

elif condition2 

then 

    command2

else

    commandN

be


Case 1:

if else single loop can be run directly on the command line

# if [ `awk -F: '/^root/ {print $3}' /etc/passwd` -eq 0 ];then ehco "true";fi


Case 2:

Double-branch loop to determine the current shell environment

# vi chkshell.sh

#!/bin/bash

if [ $SHELL = "/bin/bash" ];then

echo "your shell is bash"

else

echo "your shell is not bash but $SHELL"

be 


Case 3:

Multiple loops compare the magnitude of two numbers

# vi if1.sh

#!/bin/bash

a=10

b=20

if [ $a == $b ];then

echo "a is equal than b"

elif [ $a -gt $b ];then

echo "a is greater than b"

elif [ $a -lt $b ];then

echo "a is less than b"

else

echo "None of the condition met"

be


2. For loop

Format:

for var in item1 item2 ... itemN

do

    command1

    command2

    ...

    commandN

done


for ((expr1;expr2;expr3));do

    command1

    command2

    ...

    commandN

done


Case 1:

Calculate the sum of 1 to 100

# vi for1.sh 

#!/bin/bash

sum=0

for in in {1..100};do

let sum=$sum+$i

done

echo $sum 


Case 2:

Calculate the sum of 1 to 100

# vi for2.sh

#!/bin/bash

sum=0 

for((i=1;i<=100;i++));do 

sum=$(($sum+$i))

done

echo $sum


Three, while statement

A while loop is used to continuously execute a series of commands and also to read data from an input file; commands are usually test conditions. Its format is:

while condition

do

    command

done


Case 1:

Calculate the sum of 1 to 100

# vi wihle1.sh

#!/bin/bash

sum=0

i=1

while [ $i -le 100 ];do

((sum+=1))

((i++))

done

echo $sum


Fourth, the until statement

The until loop executes a series of commands until the condition is true and stops.

The until loop is the exact opposite of the while loop.

While loops are generally preferred over until loops, there are times—and only rare cases—that until loops are more useful.

until syntax format:

until condition

do

    command

done


condition is generally a conditional expression. If the return value is false, the statement in the loop body will continue to be executed, otherwise it will jump out of the loop.


Case 1:

Calculate the sum of 1 to 100

# vi until1.sh

#!/bin/bash

sum=0

i=1

until [ $i -gt 100 ];do 

((sum+=i))

((i++))

done 

echo $sum


Five, case statement

case loop:

The case statement is a multiple-choice statement. You can use the case statement to match a value with a pattern, and if the match succeeds, execute the matching command. The format of the case statement is as follows:

case value in

Mode 1)

    command1

    command2

    ...

    commandN

    ;;

Mode 2)

    command1

    command2

    ...

    commandN

    ;;

esac


The case works as shown above. The value must be followed by the word in, and each pattern must end with a closing parenthesis. The value can be a variable or a constant. After the match finds that the value matches a certain pattern, all commands during the period start to be executed until ;;.

The value will detect every pattern that matches. Once the pattern is matched, no other patterns will be continued after the corresponding command of the matched pattern is executed. If there is no matching pattern, use the asterisk * to capture the value, and then execute the following command.


Case 1:

Simple version of scripts for manipulating services like ./nginx start

# vi case1.sh

#!/bin/bash

case $1 in 

start)

echo "service is running"

;;

stop)

echo "service is stop"

;;

reload)

echo "service is relaod"

;;

*)

echo "usage:[start|stop|reload]"

;;

esac


# sh case1.sh start

service is running


Case 2:

Enter a character to determine whether the character is a letter, number or other

# vi case2.sh

#!/bin/bash

read -p "press one key,then press return: " KEY

case $KEY in 

[a-z]|[A-Z])

echo "It's a letter"

;;

[0-9])

echo "It's a digit"

;;

*)

echo "It's function keys,Spacebar other keys"

esac


6. Jump out of the loop

In the loop process, sometimes it is necessary to force out of the loop when the loop end condition is not reached. Shell uses two commands to achieve this function: break and continue.


6.1, break statement

The break command allows to break out of all loops (terminate execution of all subsequent loops).

Case 1:

The script goes into an infinite loop until the input character is outside of 1 to 5

# vi break1.sh

#!/bin/bash

while :

do 

echo -n "please enter 1 to 5: "

read num

case $num in

1|2|3|4|5)

echo "your input number is $num"

;;

*)

echo "your number is error,over"

break

;;

esac

done


Case 2:

Number to 5 to jump out of the loop

# vi break2.sh

#!/bin/bash

i=1 

while [ $i -lt 10 ];do 

echo $i 

if [ $i -eq 5 ]

then

break

be 

((i+=1))

done 


The result of the execution is the output

1

5


6.2, continue statement

The continue command is similar to the break command, except that it does not jump out of all loops, only the current loop.

# vi continue1.sh 

#!/bin/bash

while :

do 

echo -n "please enter 1 to 5: "

read num

case $num in

1|2|3|4|5)

echo "your input number is $num"

;;

*)

echo "your number is error"

continue

echo "over"

;;

esac

done


The result of the execution is that when characters other than 1 to 5 are entered, the loop will not be jumped out, and the statement echo "over" will not be executed.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326118753&siteId=291194637