Shell 流程控制

Shell流程控制

和Java、PHP等语言不一样,sh的流程控制不可为空,如:(以下为Java流程控制写法):

if(param.equals("test")){
  doSome(param);          
}else{
  //doNothing  
}

在sh/bash里可不能这么写,如果else分支没有语句执行,就不要写这个else。

if eles

if

if语句语法格式:

if condition
then
    command1
    command2
    ...
    command
fi

写成一行(适用于终端命令提示符)

if [ $(ps -ef | grep -c "ssh") -gt 1 ]; then echo "true"; fi

末尾的fi就是if倒过来拼写。

if else

if else 语法格式:

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

if else-if else

if else-if else 语法格式:

if condition
then
   command1
elif condition2
then
   command2
else
command3
fi

实例:

#!/bin/bash
a=10
b=20
if [ $a == $b ]
then
   echo "a 等于 b"
elif [ $a -gt $b ]
then
   echo "a 大于 b"
elif [ $a -lt $b ]
then
   echo "a 小于 b"
else
   echo "没有符合的条件"
fi

执行脚本,结果如下:

[root@localhost shell]# chmod +x if.sh
[root@localhost shell]# ./if.sh 
a 小于 b

if else 语句疆场与test来命令组合使用

num1=22
num2=32
if test $[num1] -eq $[num2]
then
   echo "两个数相等"
else
   echo 两个数不相等
fi

执行结果:

两个数不相等

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循环使用命令行的位置参数。

例如,顺序输出当前列表中的数字:

#!/bin/bash
for num in 1 2 3 4 5
do
echo "当前数字为:$num"
done

执行结果:

[root@localhost shell]# chmod +x for.sh 
[root@localhost shell]# ./for.sh 
当前数字为:1
当前数字为:2
当前数字为:3
当前数字为:4
当前数字为:5

顺序输出字符串中的字符:

for str in "This is a string!"
do
echo "当前字符为:$str"
done

string="this is another string!"
for str in $string
do
echo "当前字符为:$str"
done
string="this is the third string!"
for str in ${string[@]}
do
echo "当前字符为:$str"
done

执行结果为:

当前字符为:This is a string!
当前字符为:this
当前字符为:is
当前字符为:another
当前字符为:string!
当前字符为:this
当前字符为:is
当前字符为:the
当前字符为:third
当前字符为:string!

为什么这里直接在for后用引号括起来的字符串(单或双)循环一次得到整个字符串,而in后面是字符串变量$string或者${string[@]}、${string[*]}时字符串被按空格拆分遍历了呢???

while语句

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

while condition
do
   command
done

以下是一个基本的while循环,测试条件是:如果int小于等于5那么条件返回真。int从0开始,每次循环处理时,int加1。运行脚本,返回数字1到5然后终止。

#!/bin/bash
int=1
while [ $int -le 5 ]
do
echo $int
let "int++"
done

执行结果:

[root@localhost shell]# chmod +x while.sh 
[root@localhost shell]# ./while.sh 
1
2
3
4
5

使用中使用了Bash let命令,它用于执行一个或多个表达式,变量计算中不需要加上$来表示变量。如果表达式中包含了空格或其他特殊字符,则必须引起来。

while煦暖可用于读取键盘信息。

echo '按下 <CTRL-D> 退出'
echo -n '输入你最喜欢的电影名:'
while read FILM
do
   echo "是的,$FILM 是一部好电影。"
done

执行脚本结果:

按下 <CTRL-D> 退出
输入你最喜欢的电影名:好吃不贵
是的,好吃不贵 是一部好电影。

无限循环

while :
do
   command
done


while true
do
   command
done

for (( ; ; ))

until循环

until循环执行一些列命令直至条件为真时停止。until循环与while循环再处理方式上刚好相反

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

until语法格式:

until condition
do
   command
done

case

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

casein
模式1)
   command1
   command2
   ...
   command
   ;;
模式2)
   command1
   command2
   ...
   command
   ;;
esac

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

取值将检测匹配的每一个模式。一旦模式匹配,则执行完匹配模式相应命令后不在继续其他模式。

如无一匹配模式,使用型号*捕获该值,再执行后面的命令。

下面的脚本提示输入1到4,与每一种模式进行匹配:

#!/bin/bash
echo "请输入 1 到 4 之间的数字:"
echo "你输入的数字为:"
read num
case $num in
1) echo "你输入了 1"
;;
2) echo "你输入了 2"
;;
3) echo "你输入了 3"
;;
4) echo "你输入了 4"
;;
*) echo "你没有输入1 到 4 之间的数字!"
;;
esac

执行结果:

请输入 14 之间的数字:
你输入的数字为:
3
你输入了 3

跳出循环

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

break命令

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

 下面的例子中脚本进入死循环直至用户输入数字大于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 之间的数字,game over!"
 break
;; esac done

执行结果

[root@localhost shell]# ./break.sh 
请输入1 到 5 之间的数字:5
你输入的数字为:5
请输入1 到 5 之间的数字:d
你没有输入1 到 5 之间的数字,game over!
[root@localhost shell]# 

continue 跳出当前循环

 

posted @ 2016-08-01 14:57  GoQC  阅读(126)  评论(0)    收藏  举报