Shell编程七-Shell 流程控制

Shell 流程控制

一、if语句

if 语句语法格式:

if condition
then
    command1 
    command2
    ...
    commandN 
fi

二、if else语句

if else 语法格式:

if condition
then
    command1 
    command2
    ...
    commandN
else
    command
fi

三、if else-if else 语句
if else-if else 语法格式:

if condition1
then
    command1
elif condition2 
then 
    command2
else
    commandN
fi

实例:
#!/bin/bash
#--------------------------------------------
#name:practise23.sh
#author:wdh
#date:20181209
#--------------------------------------------

var_one=100
var_two=200

if test $[var_one] -ne $[var_two]
then
    echo "两个数不相等"
fi
echo '---------------------------------------------------'

if test $[var_one] -eq $[var_two]
then
    echo "两个数相等"
else
    echo "两个数不相等"
fi
echo '---------------------------------------------------'

if [ $var_one -eq $var_two ]
then
    echo "$var_one == $var_two"
elif [ $var_one -gt $var_two ]
then
    echo "$var_one > $var_two"
elif [ $var_one -lt $var_two ]
then
    echo "$var_one < $var_two"
fi
echo '---------------------------------------------------'

执行 ./practise22.sh
运行脚本,结果如下:

两个数不相等
---------------------------------------------------
两个数不相等
---------------------------------------------------
100 < 200
---------------------------------------------------


四、for 循环
与其他编程语言类似,Shell支持for循环。

for循环一般格式为:
for var in item1 item2 ... itemN
do
    command1
    command2
    ...
    commandN
done

写成一行:
for var in item1 item2 ... itemN; do command1; command2… done;

当变量值在列表里,for循环即执行一次所有命令,使用变量名获取列表中的当前取值。命令可为任何有效的shell命令和语句。in列表可以包含替换、字符串和文件名。

in列表是可选的,如果不用它,for循环使用命令行的位置参数。

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

while condition
do
    command
done

六、无限循环
无限循环语法格式:

while :
do
    command
done
或者

while true
do
    command
done
或者

for (( ; ; ))

七、until 循环
until 循环执行一系列命令直至条件为 true 时停止。
until 循环与 while 循环在处理方式上刚好相反。
一般 while 循环优于 until 循环,但在某些时候—也只是极少数情况下,until 循环更加有用。

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

实例:
#!/bin/bash
#--------------------------------------------
#name:practise24.sh
#author:wdh
#date:20181209
#--------------------------------------------

#for循环 #顺序输出当前列表中的数字
for loop in 1 2 3 4 5 6
do
    echo "The value is :$loop"
done

#for循环 #顺序输出字符串中的字符
for str in "C++ is the best language!"
do
    echo "The string is :$str"
done
echo '---------------------------------------------------'

#while循环
var_i=1
while (( $var_i <= 6 ))
do
    echo "The value is :$var_i"
    let "var_i++"
done
echo '---------------------------------------------------'

#while循环
echo '按下 <CTRL+D> 退出'
echo -n '输入你最喜欢的动物名: '
while read animal
do
    echo "$animal,很多人都喜欢它!"
done
echo '---------------------------------------------------'

#无限循环
var_i_two=1
while :
do
    echo "i_two:$var_i_two"
    (( var_i_two++ ))
    if test $[var_i_two] -eq 6;then break;fi
done
echo '---------------------------------------------------'

#无限循环
var_i_three=6
while true
do
    echo "i_three:$var_i_three"
    (( var_i_three++ ))
    if test $[var_i_three] -eq 11;then break;fi
done
echo '---------------------------------------------------'

#无限循环
var_i_four=1
for (( ;; ))
do
    echo "i_four:$var_i_four"
    (( var_i_four++ ))
    if test $[var_i_four] -eq 6;then break;fi
done
echo '---------------------------------------------------'

#until循环
var_i_five=6
until [ ! $var_i_five -lt 11 ]
do
    echo "i_five:$var_i_five"
    var_i_five=`expr $var_i_five + 1`
done
echo '---------------------------------------------------'

执行 ./practise24.sh
运行脚本,结果如下:

The value is :1
The value is :2
The value is :3
The value is :4
The value is :5
The value is :6
The string is :C++ is the best language!
---------------------------------------------------
The value is :1
The value is :2
The value is :3
The value is :4
The value is :5
The value is :6
---------------------------------------------------
按下 <CTRL+D> 退出
输入你最喜欢的动物名: dog
dog,很多人都喜欢它!
---------------------------------------------------
i_two:1
i_two:2
i_two:3
i_two:4
i_two:5
---------------------------------------------------
i_three:6
i_three:7
i_three:8
i_three:9
i_three:10
---------------------------------------------------
i_four:1
i_four:2
i_four:3
i_four:4
i_four:5
---------------------------------------------------
i_five:6
i_five:7
i_five:8
i_five:9
i_five:10
---------------------------------------------------


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

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

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


九、esac
case的语法和C family语言差别很大,它需要一个esac(就是case反过来)作为结束标记,每个case分支用右圆括号,用两个分号表示break

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

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

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

实例:
#!/bin/bash
#--------------------------------------------
#name:practise25.sh
#author:wdh
#date:20181209
#--------------------------------------------

#case 
echo '输入 1 到 4 之间的数字:'
echo '你输入的数字为:'
read var_i
case $var_i in
    1)  echo '你选择了 1'
    ;;
    2)  echo '你选择了 2'
    ;;
    3)  echo '你选择了 3'
    ;;
    4)  echo '你选择了 4'
    ;;
    *)  echo '你没有输入 1 到 4 之间的数字'
    ;;
esac
echo '---------------------------------------------------'

执行./practise25.sh
运行脚本,结果如下:
输入 1 到 4 之间的数字:
你输入的数字为:
1
你选择了 1

猜你喜欢

转载自blog.csdn.net/weidonghua2/article/details/84927143