shell流程控制语句

时间:2023-03-09 08:32:54
shell流程控制语句

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@-shiyan sh]# cat > for
for f in
do
echo $f
done
[root@-shiyan sh]# bash for 一定要写成两个点号,一个或多于二个都不行
[root@-shiyan sh]# cat > for
for i in {..}
do
echo $i
done
[root@-shiyan sh]# bash for ###第二种情况,可以写变量名
[root@-shiyan sh]# cat > for
IFS=':'
a="root:x:0:0:root:/root:/bin/bash"
for f in $a
do
echo $f
done
[root@-shiyan sh]# bash for
root
x root
/root
/bin/bash ###第三种情况,可以写命令
[root@-shiyan sh]# cat > for
a="/root/sh"
for f in $(ls $a)
do
echo "file-i:$f"
done
[root@-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@-shiyan sh]# cat > for
for f in `ls *.sh`
do
name=`echo "$f"|awk -F. '{print $1}'`
echo $name
done
[root@-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@-shiyan sh]# cat > for
for((i=;i<=;i++))
do
echo $(expr $i \* )
done
[root@-shiyan sh]# bash for
[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@-shiyan sh]# cat > until
f=
until [ $f -lt ]
do
echo $f
((f--))
done
[root@-shiyan sh]# bash until 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@-shiyan frag]# cat while1.sh
while [ -gt ]
do
sleep
echo used
echo "`free |awk '/Mem/{print $3}'`"
done
[root@-shiyan frag]# bash while1.sh
used used used ####另外一种形式,什么也不判断就一直循环
while :
do
sleep
echo "`df -h`"
done ####第二种情况从标准输入或文件中读取什么,输出什么
while read line
do
echo $line
done
< /etc/passwd ####第三种情况加条件判断才循环
min=
max=
while [ $min -le $max ]
do
echo $min
min=`expr $min + `
done ####第四种形式,双括号形式,内部结构有点像C的语法,注意赋值:i=$(($i+))
i=
while(($i<))
do
if(($i%==))
then
echo $i
fi
i=$(($i+))
done 循环的结束
####计数器控制的while循环
主要用于已经准确知道要输入的数据和字符串的数目。 ####结束标记控制的while循环
主要用于不知道读入数据的个数,但是可以设置一个特殊的数据值来结束循环,该特殊值称为结束标记,通过提示用户输入进行操作。
[root@-shiyan frag]# cat aa.sh
#用脚本演示使用结束标记控制while循环实现猜1~10内的数
#!/bin/sh
echo "Please input the num (1~~10): "
read num
while [[ $num != ]]
do
if [ $num -lt ]
then
echo "Too small ,Try again.."
read num
elif [ $num -gt ]
then
echo "Too big ,Try again.. "
read num
else
exit
fi
done
echo "Yes ,you are right !!"
[root@-shiyan frag]# bash aa.sh
Please input the num (~~): Too small ,Try again.. Yes ,you are right !! ####标致控制的while循环
用户输入标志值来控制循环结束
[root@-shiyan frag]# cat a1.sh
#!/bin/sh
echo "Please input the num:"
read num
sum=
i=
signal=
while [[ $signal != ]]
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@-shiyan frag]# bash a1.sh
Please input the num: +、、、+=
[root@-shiyan frag]# bash a1.sh
Please input the num: +、、、+= ####命令行控制的while循环
[root@-shiyan frag]# cat aa.sh
#!/bin/sh
echo "Please input arguements is $# "
echo "What you input : "
while [[ $* != "" ]]
do
echo $
shift
done
[root@-shiyan frag]# ./aa.sh
Please input arguements is
What you input :

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,,,4列出菜单 (没有echo指令,自动显示菜单)
()、自动read输入选择 (没有 read指令,自动输入)
()、赋值给变量 (没有赋值指令,自动输入数字后,赋值字符串给变量)
select本身就是一个循环,break是当选择后,就跳出循环
虽然select本身就是循环,但不建议用他的循环 ,因为select虽然循环却不再显示菜单,只循环输入,所以select 语句干脆直接用break,只执行一次,在其上另配while循环,这样以后菜单也跟着循环。select是循环选择,一般与case语句使用。 [root@-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 ;;
*) 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;}')

real    0m0.003s
user 0m0.003s
sys 0m0.000s
[chengmo@localhost nginx]# time(total=;for i in $(seq );do total=$(($total+i));done;echo $total;) real 0m0.141s
user 0m0.125s
sys 0m0.008s 实现相同功能,可以看到awk实现的性能是shell的50倍!