Shell编程-控制结构 | 基础篇

时间:2023-12-17 22:37:14

if-then-else分支结构

if-then-else是一种基于条件测试结果的流程控制结构。如果测试结果为真,则执行控制结构中相应的命令列表;否则将进行另外一个条件测试或者退出该控制结构。

if-then-else语法格式:

if 条件1
then 命令列表1
elif 条件2
then 命令列表2
else 命令列表3
fi

Shell编程-控制结构 | 基础篇

执行逻辑流程图

说明:当条件1成立时,则执行命令列表1并退出if-then-else控制结构;如果条件2成立,则执行命令列表2并退出if-then-else控制结构;否则执行命令列表3并退出if-then-else控制结构。在同一个if-then-else结构中只能有一条if语句和一条else语句,eilf语句可以有多条。其中if语句是必须的,elif和else语句是可选的。

Shell脚本首先判断文件test1是否可读,如果是,则输出 is readable !的提示信息;否则不进行任何动作。

[root@localhost ]# vi test.sh
filename=test1
if [ -r $filename ] //输出test1可读则输出信息
then
echo $filename' is readable !'
fi
[root@localhost ]# sh test.sh
test1 is readable !

Shell脚本会判断number变量是否等于100,如果是,则输出 The number is equal 100 !的提示;否则输出 The number is not equal 100 !。

[root@localhost ]# vi number.sh
number=
if [ $number -eq ] //如果number等于100则输出“The number is equal 100 !”提示
then
echo 'The number is equal 100 !'
else //否则输出“The number is not equal 100 !”提示
echo 'The number is not equal 100 !'
fi
[root@localhost ]# sh number.sh
The number is not equal !

Shell脚本首先判断number变量是否小于10,如果是则输出 The number < 10 !;否则,判断number变量是否大于等于10且小于20。如果是则输出 10 =< The number < 20 !;否则,判断 number变量是否大于等于20且小于30。如果是,则输出 20 =< The number < 30 !;否则,输出 30 <= The number !。

[root@localhost ]# vi number1.sh
number=
if [ $number -lt ] //如果number小于10
then
echo 'The number < 10 !'
elif [ $number -ge -a $number -lt ] //如果number大于等于10且小于20
then
echo '10 =< The number < 20 !'
elif [ $number -ge -a $number -lt ] //如果number大于等于20且小于30
then
echo '20 =< The number < 30 !'
else //除上述3种情况以外的其他情况
echo '30 <= The number !'
fi
[root@localhost ]# sh number1.sh
=< The number < !

case分支结构

if-then-else结构能够支持多路的分支(多个elif语句),但如果有多个分支,那么程序就会变得难以阅读。case结构提供了实现多路分支的一种更简洁的方法。

case语法格式:

case 值或变量 in
模式1)
命令列表1
;;
模式2)
命令列表2
;;
...
esac

case语句后是需要进行测试的值或者变量。Shell会顺序地把需要测试的值或变量与case结构中指定的模式逐一进行比较,当匹配成功时,则执行该模式相应的命令列表并退出case结构(每个命令列表以两个分号“;;”作为结束)。如果没有发现匹配的模式,则会在esac后退出case结构。

如下该脚本对number变量的值进行测试,与模式匹配的话,则输出相应的信息。

[root@localhost ]# vi case.sh
number=
case $number in
) echo 'The number is 33 !' //number 变量等于 33
;;
) echo 'The number is 44 !' //number 变量等于 44
;;
) echo 'The number is 55 !' //number 变量等于 55
;;
) echo 'The number is 66 !' //number 变量等于 66
;;
) echo 'The number is 77 !' //number 变量等于 77
;;
) echo 'The number is 88 !' //number 变量等于 88
;;
esac //结束 case 结构
[root@localhost ]# sh case.sh
The number is ! //命令的输出结果

for循环结构

for循环结构可以重复执行一个命令列表,基于for语句中所指定的值列表决定是继续循环还是跳出循环。for循环在执行命令列表前会先检查值列表中是否还有未被使用的值,如有的话,则把该值赋给for语句中指定的变量,然后执行循环结构中的命令列表。如此循环,直到值列表中的所有值都被使用。

for循环结构语法:

for 变量名 in 值列表
do
命令1
命令2
命令3
...
done
  • 以常量作为值列表

使用变量1、2、3、4、5、6作为值列表,for循环中只是简单的把值列表中的值进行输出。

[root@localhost ]# vi for1.sh
#!/bin/bash
for n in //循环读取 1-6
do
echo $n
done
由运行结果可以非常清楚的了解for循环的运行过程。
[root@localhost ]# sh for1.sh
  • 以变量作为值列表

值列表可以是一个环境变量。

[root@localhost ]# vi for2.sh
#!/bin/bash
values="1 2 3 4 5 6" //对 values 变量赋值
for n in $values //循环读取 values 变量中的值
do
echo $n
done
[root@localhost ]# sh for2.sh
  • 以命令运行结果作为值列表

Shell支持使用命令的运行结果作为for循环的值列表。在Shell中通过"`命令`"或者“$(命令)”来引用命令的运行结果。将会以ls命令的结果作为值列表。

[root@localhost ]# vi for3.sh
#!/bin/bash
for n in `ls` //循环读取 ls 命令的输出结果
do
echo $n //输出变量 n 的值
done
[root@localhost ]# sh for3.sh
case.sh
for1.sh
for2.sh
for3.sh
HelloWorld.sh
number1.sh
number.sh
test1
test2
test.sh

expr命令计算器

expr是一个命令行的计数器,用于加、减、乘、除运算。

[root@localhost ]# expr  +  -   //123 加 456 减 78 等于 501

[root@localhost ]# expr  \*        //9 乘以 8 等于 72

[root@localhost ]# expr  /       // 666 除以 8 等于 83

在循环结构中,expr 会被用作增量计算,初始值为10,每次使用expr增加加11/12。注意:这里使用expr命令时都使用的是反撇号,不是单引号。

[root@localhost ]# number=
[root@localhost ]# number=`expr $number + ` //对number变量的值加11
[root@localhost ]# echo $number [root@localhost ]# number=`expr $number + ` //对number变量的值加12
[root@localhost ]# echo $number

while循环结构

while结构会循环执行一系列的命令,并基于while语句中所指定的测试条件决定是继续循环还是跳出循环。如果条件为真,则while循环会执行结构中的一系列命令。命令执行完毕后,控制返回循环顶部,从头开始重新执行直到测试条件为假。

while循环结构的语法:

while 条件
do
命令1
命令2
...
done
  • 循环增量计算:是在while循环中使用增量计算,其运行结果如下。

[root@localhost ]# vi while1.sh
#!/bin/bash
count= //将 count 变量置 0
#当 count 变量小于5时继续循环
while [ $count -lt ]
do
#每循环一次,count 变量的值加1
count=`expr $count + `
echo $count
done
[root@localhost ]# sh while1.sh [root@localhost ]#
  • 循环从文件中读取内容

现有一文件,保存了学生的成绩信息,其中第一列是学生名,第二列是学生的成绩。

[root@localhost ]# vi students.log
jake
tom
lucy
sam

现在要对以上文件中的学生成绩进行统计,计算学生的数量以及学生的平均成绩。通过 while read 语句读取变量 STUDENT 和 SCORE 的内容,然后在 while 循环中通过 expr 命令计算学生总数和学生总成绩,最后计算平均值并输出。执行该脚本时需要把 students.log 文件的内容重定向到 while2.sh脚本中。

[root@localhost ]# vi while2.sh
#!/bin/bash
TOTAL= //将变量 TOTAL 置 0
COUNT= //将变量 COUNT 置 0
#循环读取数据
while read STUDENT SCORE
do
#计算总成绩
TOTAL=`expr $TOTAL + $SCORE`
#计算学生数
COUNT=`expr $COUNT + `
done
#计算平均成绩
AVG=`expr $TOTAL / $COUNT`
echo 'There are '$COUNT' students , the avg score is '$AVG
[root@localhost ]# sh while2.sh < students.log
There are students , the avg score is
[root@localhost ]#

until循环结构

until是除 for 和 while以外的一种循环结构,它会循环执行一系列命令直到条件为真时停止。

until循环结构语法:

until 条件
do
命令1
命令2
...
done

until循环中读取用户输入的内容并显示到屏幕上,当用户输入的内容为 exit 时结束循环。

[root@localhost ]# vi until1.sh
#!/bin/bash
xxx=""
#当 ans 变量的值为 exit 时结束循环
until [ "$xxx" = exit ]
do
#读取用户的输入到ans变量
read xxx
#如果用户输入的不是 exit 则输出用户的输入
if [ "$xxx" != exit ]
then
echo 'The user input is : '$xxx
#否则退出循环
else
echo 'Exit the script.'
fi
done
[root@localhost ]# sh until1.sh
hello
The user input is : hello
welcome to HongKong!
The user input is : welcome to HongKong!
exit
Exit the script.
[root@localhost ]#