今天我们来介绍几个流程控制语句。
if else语句
1、if语句语法格式
if condition
then
command1
command2
......
commandn
fi
也可以写成:
if condition;then
command1
command2
......
commandn
fi
2、if else语句语法格式
if condition
then
command1
command2
...
commandn
else
command
fi
3、if else-if else语句语法格式
if condition1
then
command1
elif condition2
then
command2
else
commandn
fi
接下来我们通过脚本实现一下:
例1:编写一个脚本,输入1~10之间的一个数,并判断它是否小于5.
脚本内容为:
#!/bin/bash
echo 'please key in a number(1-10)'
read a
if [ "$a" -lt 1 -o "$a" -gt 10 ]
then
echo "Error Number."
elif [ ! "$a" -lt 5 ]
then
echo "It's not less 5."
else
echo "It's less 5."
fi
脚本执行结果为:
[root@localhost ~]# bash lt
please key in a number(1-10)
20
Error Number.
[root@localhost ~]# bash lt
please key in a number(1-10)
3
It's less 5.
#It 为脚本名称
for循环
for循环的一般格式为:
for variable in [ argument-list ]
do
command1
command2
...
commandn
done
[ argument-list ]为可选项,其可以有三种形式:变量值表、文件表达式、空。
for people in Tom John Kitty #变量值表
......
for i in *.c #文件表达式,意为当前目录下的所有以.c结尾的文件
......
for name (in $*) #其中$*和空的意思等价,括号内的东西可省略,执行过程是变量依次取位置参数的值,然后执行循环体中的命令表,直至所有参数取完为止。
例2:编写shell脚本,第一个位置参数为指定的目录,其后指定的位置参数为第一个位置参数指定目录下的文件,显示这些文件的内容。
脚本内容为:
#!/bin/bash
dir=$1;shift
if [ -d $dir ]
then
cd $dir
for name
do
if [ -f $name ]
then
cat $name
echo "End of ${dir}/$name"
else
echo "Invaild file name:${dir}/$name"
fi
done
else
echo "Bad directory name:$dir"
fi
执行结果为:
[root@localhost ~]# bash for.sh /root/test a b c
this is a
End of /root/test/a
this is b
End of /root/test/b
this is c
End of /root/test/c
for循环还可以这么使用:
for((assignment;condition:next));do
command_1;
command_2;
commond_..;
done;
如上所示,这里的 for 循环与 C 中的相似,但并不完全相同。
通常情况下 shell 变量调用需要加 $,但是 for 的 (()) 中不需要,但是如果要在循环体中进行 for 中的 next 操作,记得变量要加 $,不然程序会变成死循环。
while语句
while循环用于不断执行一系列命令,也用于从输入文件中读取数据;命令通常为测试条件,其格式为:
while condition
do
command
done
#只有满足条件才进入循环,直至不满足时退出。
例3:利用while循环输出1到5之间的整数。
脚本内容为:
#!/bin/bash
x=1
while [ $x -le 5 ]
do
echo $x
x=`expr $x + 1`
done
执行结果为:
1
2
3
4
5
无限循环
无限循环语法格式为:
while :
do
command
done
或者
while true
do
command
done
或者
for (( ; ; ))
until循环
在until语句中,只在表达式为假时才执行循环体;
一般 while 循环优于 until 循环,但在某些时候—也只是极少数情况下,until 循环更加有用。
例4:利用until循环输出1到5之间的整数。
脚本内容为:
#!/bin/bash
x=1
until [ $x -gt 5 ]
do
echo $x
x=`expr $x + 1`
done
执行结果和例3相同。
case语句
shell中的case语句为多选择语句,从第一个条件开始,如果满足哪一个条件,就执行其命令,若满足多个,也只执行满足的第一个。
case语句格式为:
case 值 in
condition1)
command
;;
condition2)
command
;;
......
conditionn)
command
;;
*) #若以上条件都不满足,则执行该语句,可省略
command
;;
esac
例5:用脚本提示输入1到4,与每一个模式进行匹配。
脚本内容为:
#!/bin/bash
echo '输入 1 到 4 之间的数字:'
echo '你输入的数字为:'
read aNum
case $aNum in
1) echo '你选择了 1'
;;
2) echo '你选择了 2'
;;
3) echo '你选择了 3'
;;
4) echo '你选择了 4'
;;
*) echo '你没有输入 1 到 4 之间的数字'
;;
esac
执行结果为:
输入 1 到 4 之间的数字:
你输入的数字为:
2
你选择了 2
break语句和continue语句
和其他编程语言一样,break命令将控制转移到done后面的命令,因此循环提前结束。而continue命令将控制转移到done,接着再次计算条件的值,以决定是否继续循环。
1、break语句(跳出所有循环)
例6:脚本进入死循环直至用户输入数字大于5。要跳出这个循环,返回到shell提示符下,需要使用break命令。
脚本内容为:
#!/bin/bash
while :
do
echo -n "输入 1 到 5 之间的数字:"
read num
case $num in
1|2|3|4|5) echo "你输入的数字为 $num!"
;;
*) echo "你输入的数字不是 1 到 5 之间的! "
break
;;
esac
done
执行结果为:
输入 1 到 5 之间的数字:3
你输入的数字为 3!
输入 1 到 5 之间的数字:4
你输入的数字为 4!
输入 1 到 5 之间的数字:5
你输入的数字为 5!
输入 1 到 5 之间的数字:6
你输入的数字不是 1 到 5 之间的!
2、continue语句(不会跳所有循环,仅跳出当前循环)
例7:跟例6的脚本大致相同
脚本内容为:
#!/bin/bash
while :
do
echo -n "输入 1 到 5 之间的数字:"
read num
case $num in
1|2|3|4|5) echo "你输入的数字为 $num!"
;;
*) echo "你输入的数字不是 1 到 5 之间的!"
continue
;;
esac
done
执行结果为:
输入 1 到 5 之间的数字:2
你输入的数字为 2!
输入 1 到 5 之间的数字:3
你输入的数字为 3!
输入 1 到 5 之间的数字:5
你输入的数字为 5!
输入 1 到 5 之间的数字:6
你输入的数字不是 1 到 5 之间的!
输入 1 到 5 之间的数字:^C
最后的ctrl+c是我退出脚本用的。1、
我们会发现不管输入的数是否满足条件,continue都会继续执行下一条语句。