一、if循环

语法格式如下:


单分支的if语句:


if condition

then

    command1 

    command2

    ...

    commandN 

fi


双分支的if语句:


if condition

then

    command1 

    command2

    ...

    commandN

else

    command

fi

 


多分支的if语句:


if condition1

then

    command1

elif condition2 

then 

    command2

else

    commandN

fi


案例1:

if else单支循环可以直接在命令行组合运行

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


案例2:

双支循环判断当前shell环境

# vi chkshell.sh

#!/bin/bash

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

echo "your shell is bash"

else

echo "your shell is not bash but $SHELL"

fi 


案例3:

多支循环比较两个数的大小

# 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"

fi


二、for循环

格式:

for var in item1 item2 ... itemN

do

    command1

    command2

    ...

    commandN

done


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

    command1

    command2

    ...

    commandN

done


案例1:

计算1到100的和

# vi for1.sh 

#!/bin/bash

sum=0

for in in {1..100};do

let sum=$sum+$i

done

echo $sum 


案例2:

计算1到100的和

# vi for2.sh

#!/bin/bash

sum=0 

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

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

done

echo $sum


三、while 语句

while循环用于不断执行一系列命令,也用于从输入文件中读取数据;命令通常为测试条件。其格式为:

while condition

do

    command

done


案例1:

计算1到100的和

# vi wihle1.sh

#!/bin/bash

sum=0

i=1

while [ $i -le 100 ];do

((sum+=1))

((i++))

done

echo $sum


四、until语句

until 循环执行一系列命令直至条件为 true 时停止。

until 循环与 while 循环在处理方式上刚好相反。

一般 while 循环优于 until 循环,但在某些时候—也只是极少数情况下,until 循环更加有用。

until 语法格式:

until condition

do

    command

done


condition 一般为条件表达式,如果返回值为 false,则继续执行循环体内的语句,否则跳出循环。


案例1:

计算1到100的和

# vi until1.sh

#!/bin/bash

sum=0

i=1

until [ $i -gt 100 ];do 

((sum+=i))

((i++))

done 

echo $sum


五、case语句

case循环:

case语句为多选择语句。可以用case语句匹配一个值与一个模式,如果匹配成功,执行相匹配的命令。case语句格式如下:

case 值 in

模式1)

    command1

    command2

    ...

    commandN

    ;;

模式2)

    command1

    command2

    ...

    commandN

    ;;

esac


case工作方式如上所示。取值后面必须为单词in,每一模式必须以右括号结束。取值可以为变量或常数。匹配发现取值符合某一模式后,其间所有命令开始执行直至 ;;。

取值将检测匹配的每一个模式。一旦模式匹配,则执行完匹配模式相应命令后不再继续其他模式。如果无一匹配模式,使用星号 * 捕获该值,再执行后面的命令。


案例1:

类似./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


案例2:

输入一个字符,判断该字符是字母、数字或是其他

# 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


六、跳出循环

在循环过程中,有时候需要在未达到循环结束条件时强制跳出循环,Shell使用两个命令来实现该功能:break和continue。


6.1、break语句

break命令允许跳出所有循环(终止执行后面的所有循环)。

案例1:

脚本进入死循环,直到输入的字符在1到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


案例2:

数字到5跳出循环

# vi break2.sh

#!/bin/bash

i=1 

while [ $i -lt 10 ];do 

echo $i 

if [ $i -eq 5 ]

then

break

fi 

((i+=1))

done 


执行结果就是输出

1

5


6.2、continue语句

continue命令与break命令类似,只有一点差别,它不会跳出所有循环,仅仅跳出当前循环。

# 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


执行结果是,当输入1到5之外的字符时不会跳出循环,语句 echo "over" 不会执行。