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倍!

shell流程控制语句的更多相关文章

  1. Linux Shell 流程控制语句

    * 本文主要介绍一些Linux Shell 常用的流程控制语句* 1. if 条件语句:if-then/if-elif-fi/if- else-fi if [条件判断逻辑1];then command ...

  2. 5.Shell 流程控制语句

    1.流程控制语句 通过if.for.while.case这4种流程控制语句来学习编写难度更大.功能更强的Shell脚本 4.3.1 if条件测试语句: if条件测试语句可以让脚本根据实际情况自动执行相 ...

  3. Linux | Shell流程控制语句

    流程控制语句 简单的Shell 脚本还不能满足我们日常工作的需要要,因为他不能批量的帮我们完成工作,所以Shell引入了 if.for.while.case 4种流程控制语句来帮助我们完成工作. if ...

  4. shell 流程控制语句

    case语句 case $变量名 in "值1")   如果变量的值等于值1,则执行程序1 ;;  "值2")   如果变量的值等于值2,则执行程序2 ;;   ...

  5. Shell流程控制语句for

    for语法格式: for 变量 in 参数列表 do 命令 done 或者 for 变量 in 参数列表 ; do 命令 done for语句流程控制图: 实例: [root@youxi1 ~]# v ...

  6. Shell流程控制语句while

    while语法格式: while 判断条件 do 命令 done while语句流程控制图: 实例: [root@youxi1 ~]# vim a.sh #!/bin/bash i=0 while [ ...

  7. Shell流程控制语句case

    case语法格式: case 变量或表达式 in 变量或表达式1) 命令1 ;; 变量或表达式2) 命令2 ;; ...... *) 默认命令 esac case语句流程控制图:  实例: [root ...

  8. Shell流程控制语句if

    (1).if语句 语法格式: if 判断条件 ; then 命令 fi 或 if 判断条件 then 命令 fi if语句流程图: 实例:判断命令是否执行成功,成功则输出语句This is ok. [ ...

  9. linux shell awk 流程控制语句(if,for,while,do)详细介绍

    在linux awk的 while.do-while和for语句中允许使用break,continue语句来控制流程走向,也允许使用exit这样的语句来退出.break中断当前正在执行的循环并跳到循环 ...

随机推荐

  1. 怎么在官网下载jstl【配图详解】

    JSTL(JSP Standard Tag Library,JSP标准标签库)是一个非常优秀的开源JSP标签库,如果要在系统使用JSTL标签,则必须将jstl.jar和 standard.jar文件放 ...

  2. php大力力 [031节] php设计系统后台菜单和样式设计

    php大力力 [031节] php设计系统后台菜单和样式设计 耗掉我一整夜的时间,把后台html设计了一个,对于我这样的html白痴,实属不容易啊. 留下一点点网上查找的网页知识: 索马里论坛群发简介 ...

  3. 修复Dll文件

    for %1 in (%windir%\system32\*.dll) do regsvr32.exe /s %1

  4. 查单神器v1.0 升级 →B站看鬼畜神器v1.0

    去年学校实习,我去了一家快递输单公司工作.工作任务就是把运单图片上的内容(寄件人,地址之类)输入到公司的数据库里.每天输单结束后,还要对一些容易错的运单进行排查.单量多的时候一天甚至要查千张以上的运单 ...

  5. HDU5772 (最小割)

    Problem String problem (HDU5772) 题目大意 给定一个由数字组成的字符串(n<=100),挑选出一些字符组成一个新的字符串. 字符串的价值: sigma w[id( ...

  6. HDU 4927

    http://acm.hdu.edu.cn/showproblem.php?pid=4927 直接模拟会超时,要在纸上写写推公式 A[n]*C(0,n-1)  - A[n-1]*C(1,n-1) + ...

  7. json-c-0.9 的简单用法

    1.下载安装包路径: wget http://oss.metaparadigm.com/json-c/json-c-0.9.tar.gz 2.解压安装包 tar zxvf json-c-0.9.tar ...

  8. CentOS 7.0 安装go 1.3.1

    1.下载go安装包 golang中国上下载 2. 解压 tar -zxf go1.3.1.linux-amd64.tar.gz -C /usr/local/ 3. 修改 etc/profile 文件在 ...

  9. 关于KVM的几篇细节文档

    1. Qemu  Study http://lists.gnu.org/archive/html/qemu-devel/2011-04/pdfhC5rVdz7U8.pdf http://handboo ...

  10. Open vSwitch简述

    一.基础术语 1.Packet (数据包):网络转发的最小数据单元,每个包都来自某个端口,最终会被发往一个或多个目标端口,转发数据包的过程就是网络的唯一功能. 2.Bridge (网桥):Open v ...