您的位置:首页 > 运维架构 > Shell

shell流程控制语句

2014-06-09 17:28 447 查看
linux shell有一套自己的流程控制语句,其中包括条件语句(if),循环语句(for,while,until),选择语句(case/select)。下面我将通过例子介绍下,各个语句使用方法。

1:在shell 中$() 与 ``等效。执行中间包含的命令语句,返回执行结果。
2:从效率来说let==(()) > expr > bc。let和(())运行是内建命令,使用相同的算法。
3:let 和 expr 的运算是整数运算,不包括浮点预算。
4:expr和bc是外部程序,expr的体积几乎等于bc的1/3,执行一次装入内存所消耗的时间就不一样。
5:从运算能力来说,bc排第一位。

shell的循环主要有3种,for,while,until
shell的分支判断主要有2种,if,case

linux shell “(())” 双括号运算符使用

shell编程控制结构:expr、let、for、while、until、shift、if、case、break、continue、函数、select

####循环控制语句 ,break continue exit return
break 命令不执行当前循环体内break下面的语句从当前循环退出.
continue 命令是程序在本循体内忽略下面的语句,从循环头开始执行
break
结束并退出循环
continue
在循环中不执行continue下面的代码,转而进入下一轮循环
exit
退出脚本,
常带一个整数给系统,如 exit 0
return
在函数中将数据返回
或返回一个结果给调用函数的脚本

if条件

#if语句的后面是Shell命令,如果该命令执行成功返回0,则执行then后面的命令。
if command
then
command
command
fi
#用test命令测试其后面expression的结果,如果为真,则执行then后面的命令。
if test expression
then
command
fi
#下面的格式和test expression等同
if [ string/numeric expression ]
then
command
fi
#下面的两种格式也可以用于判断语句的条件表达式,而且它们也是目前比较常用的两种。
if [[ string expression ]] #这种方式支持通配符,上面的那种不支持
then
command
fi

if (( numeric expression )) #let表达式
then
command
fi


for循环  http://blog.csdn.net/qiudakun/article/details/7063559

将所有的for写入一个脚本中

for i in f1 f2 f3
for i in {1..5}
for i in $a
for i in $(ls $a)
for i in $(seq 100)
for i in `ls *.sh`
for i in `seq 100`
for ((i=0;i<5;i++))
for i in ${arr[@]}
for i in $*
for f in /proc/sys/net/ipv4/conf/*/arp_accept

默认分隔符是空格,而不管是几个空格。多个空格也当一个来处理。
第一种情况,可以直接写入字符串
[root@250-shiyan sh]# cat > for
for f in 1 2 3  4  5
do
echo $f
done
[root@250-shiyan sh]# bash for
1
2
3
4
5

一定要写成两个点号,一个或多于二个都不行
[root@250-shiyan sh]# cat > for
for i in {1..5}
do
echo $i
done
[root@250-shiyan sh]# bash for
1
2
3
4
5

###第二种情况,可以写变量名
[root@250-shiyan sh]# cat > for
IFS=':'
a="root:x:0:0:root:/root:/bin/bash"
for f in $a
do
echo $f
done
[root@250-shiyan sh]# bash for
root
x
0
0
root
/root
/bin/bash

###第三种情况,可以写命令
[root@250-shiyan sh]# cat > for
a="/root/sh"
for f in $(ls $a)
do
echo "file-i:$f"
done
[root@250-shiyan sh]# bash for
file-i:aa
file-i:ab
file-i:ac
file-i:awk
file-i:ccc
file-i:cfont
file-i:check-root.sh


[root@250-shiyan sh]# cat > for
for f in `ls *.sh`
do
name=`echo "$f"|awk -F. '{print $1}'`
echo $name
done
[root@250-shiyan sh]# bash for
check-root
eth
for
ser

###查找循环(ls数据量太大的时候也可以用这种方法)
[root@250-shiyan sh]# cat > for
for f in `find . -type f -name "*.sh"`
do
name=`echo "$f"|awk -F/ '{print $2}'`
echo $name
done
[root@250-shiyan sh]# bash for
eth.sh
ser.sh
check-root.sh
for.sh

[root@250-shiyan sh]# cat > for
for f in `seq 100`  或者for f in $(seq 100)
do
if((f%4==0))
then
echo $f
continue
fi
done
[root@250-shiyan sh]# bash for
4
8
12
16
20
24



###第四种情况,for((赋值;条件;运算语句)),c语法的for循环形式
[root@250-shiyan sh]# cat > for
for((i=0;i<=5;i++))
do
echo $(expr $i \* 3)
done
[root@250-shiyan sh]# bash for
0
3
6
9
12
15

[root@250-shiyan sh]# cat > for
for((i=1;i<100;i++))
do
if((i%3==0))
then
echo $i
continue
fi
done
[root@250-shiyan sh]# bash for
3
6
9
12
15
18

[root@250-shiyan sh]# cat > for
arr=("a" "b" "c")
for f in ${arr[@]}
do
echo $f
done
[root@250-shiyan sh]# bash for
a
b
c

[root@250-shiyan sh]# cat > for
for f in $*
do
echo $f
done
[root@250-shiyan sh]# chmod u+x for
[root@250-shiyan sh]# ./for a b c d e f
a
b
c
d
e
f

[root@250-shiyan sh]# cat > for
for f in /proc/sys/net/ipv4/conf/*/arp_accept
do
echo $f
done
[root@250-shiyan sh]#
[root@250-shiyan sh]# bash for
/proc/sys/net/ipv4/conf/all/arp_accept
/proc/sys/net/ipv4/conf/default/arp_accept
/proc/sys/net/ipv4/conf/eth0/arp_accept
/proc/sys/net/ipv4/conf/lo/arp_accept

###直到满足条件,就退出。否则执行echo
[root@250-shiyan sh]# cat > until
f=5
until [ $f -lt 0 ]
do
echo $f
((f--))
done
[root@250-shiyan sh]# bash until
5
4
3
2
1
0
[root@84-monitor test]# bash loop
pass 1 in outer_loop
-------------------------
pass 1 in inner_loop
pass 2 in inner_loop
pass 3 in inner_loop
pass 4 in inner_loop
pass 5 in inner_loop

pass 2 in outer_loop
-------------------------
pass 1 in inner_loop
pass 2 in inner_loop
pass 3 in inner_loop
pass 4 in inner_loop
pass 5 in inner_loop

pass 3 in outer_loop
-------------------------
pass 1 in inner_loop
pass 2 in inner_loop
pass 3 in inner_loop
pass 4 in inner_loop
pass 5 in inner_loop

pass 4 in outer_loop
-------------------------
pass 1 in inner_loop
pass 2 in inner_loop
pass 3 in inner_loop
pass 4 in inner_loop
pass 5 in inner_loop

pass 5 in outer_loop
-------------------------
pass 1 in inner_loop
pass 2 in inner_loop
pass 3 in inner_loop
pass 4 in inner_loop
pass 5 in inner_loop

[root@84-monitor test]# cat loop
outer=1
for a in 1 2 3 4 5
do
echo "pass $outer in outer_loop"
echo "-------------------------"
inner=1
for b in 1 2 3 4 5
do
echo "pass $inner in inner_loop"
let "inner+=1"
done
let "outer+=1"
echo
done
[root@84-monitor test]#


####第一种情况,还做了一个判断才死循环
[root@250-shiyan frag]# cat while1.sh
while [ 1 -gt 0 ]
do
sleep 3
echo used
echo "`free |awk '/Mem/{print $3}'`"
done
[root@250-shiyan frag]# bash while1.sh
used
328200
used
328060
used
328060
####另外一种形式,什么也不判断就一直循环
while :
do
sleep 2
echo "`df -h`"
done

####第二种情况从标准输入或文件中读取什么,输出什么
while read line
do
echo $line
done
< /etc/passwd

####第三种情况加条件判断才循环
min=1
max=100
while [ $min -le $max ]
do
echo $min
min=`expr $min + 5`
done

####第四种形式,双括号形式,内部结构有点像C的语法,注意赋值:i=$(($i+1))
i=1
while(($i<100))
do
if(($i%4==0))
then
echo $i
fi
i=$(($i+1))
done

循环的结束


####计数器控制的while循环
主要用于已经准确知道要输入的数据和字符串的数目。

####结束标记控制的while循环
主要用于不知道读入数据的个数,但是可以设置一个特殊的数据值来结束循环,该特殊值称为结束标记,通过提示用户输入进行操作。
[root@250-shiyan frag]# cat aa.sh
#用脚本演示使用结束标记控制while循环实现猜1~10内的数
#!/bin/sh
echo "Please input the num (1~~10): "
read num
while [[ $num != 4 ]]
do
if [ $num -lt 4 ]
then
echo "Too small ,Try again.."
read num
elif [ $num -gt 4 ]
then
echo "Too big ,Try again.. "
read num
else
exit 0
fi
done
echo "Yes ,you are right !!"
[root@250-shiyan frag]# bash aa.sh
Please input the num (1~~10):
3
Too small ,Try again..
4
Yes ,you are right !!

####标致控制的while循环
用户输入标志值来控制循环结束
[root@250-shiyan frag]# cat a1.sh
#!/bin/sh
echo "Please input the num:"
read num
sum=0
i=1
signal=0
while [[ $signal != 1 ]]
do
if [ $i -eq $num ]
then
let "signal=1"
let "sum+=i"
echo "1+2、、、+$num=$sum"
else
let "sum=sum+i"
let "i++"
fi
done
[root@250-shiyan frag]# bash a1.sh
Please input the num:
1
1+2、、、+1=1
[root@250-shiyan frag]# bash a1.sh
Please input the num:
3
1+2、、、+3=6

####命令行控制的while循环
[root@250-shiyan frag]# cat aa.sh
#!/bin/sh
echo "Please input arguements is $# "
echo "What you input : "
while [[ $* != "" ]]
do
echo $1
shift
done
[root@250-shiyan frag]# ./aa.sh 34 535 232
Please input arguements is 3
What you input :
34
535
232




case格式
[root@localhost script]# cat >yesno
#!/bin/bash
echo "enter [y/n]:"
read a
case $a in
y|Y|yes|YES)
echo "you enter $a"
;;
n|N|no|NO)
echo "you enter $a"
;;
*)
echo "error"
;;
esac

它能够把变量的内容与多个模板进行匹配,再根据成功匹配的模板去决定应该执行哪部分代码。case语句的匹配是从上往下地匹配顺序。因此,case语句编写的原则是从上往下,模板从特殊到普通。在C语言里,case语句中有default模板,而在shell程序设计中,可能将模板写成*,就可以完成相同的功能。
case语句适用于需要进行多重分支的应用情况。

select表达式是一种bash的扩展应用,动作包括:
(1)、自动用1,2,3,4列出菜单 (没有echo指令,自动显示菜单)
(2)、自动read输入选择 (没有 read指令,自动输入)
(3)、赋值给变量  (没有赋值指令,自动输入数字后,赋值字符串给变量)
select本身就是一个循环,break是当选择后,就跳出循环
虽然select本身就是循环,但不建议用他的循环 ,因为select虽然循环却不再显示菜单,只循环输入,所以select 语句干脆直接用break,只执行一次,在其上另配while循环,这样以后菜单也跟着循环。select是循环选择,一般与case语句使用。

[root@250-shiyan sh]# cat sel1
#!/bin/bash
while echo "display current systeminfo"
do
select vi in "ifconfig" "date" "uptime" "quit"
do
case $vi in      case变量输入值与菜单项是一致的
"ifconfig")  ifconfig;;
"date")  date;;
"uptime")  uptime;;
"quit") exit 0;;
*) continue;;
esac
break
done
done


性能比较 awk的流程控制语句与shell的流程控制语句


[chengmo@localhost nginx]# time (awk 'BEGIN{ total=0;for(i=0;i<=10000;i++){total+=i;}print total;}')
50005000

real    0m0.003s
user    0m0.003s
sys     0m0.000s
[chengmo@localhost nginx]# time(total=0;for i in $(seq 10000);do total=$(($total+i));done;echo $total;)
50005000

real    0m0.141s
user    0m0.125s
sys     0m0.008s

实现相同功能,可以看到awk实现的性能是shell的50倍!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: