2019独角兽企业重金招聘Python工程师标准>>>

Shell编程中循环命令用于特定条件下决定某些语句重复执行的控制方式,有三种常用的循环语句:for、while和until。while循环和for循环属于“当型循环”,而until属于“直到型循环”。循环控制符:break和continue控制流程转向。

一、while语句结构

while循环用于不断执行一系列命令,也用于从输入文件中读取数据,其格式为:

//while 命令
do
命令1
命令2
. . .
done

虽然通常只使用一个命令,但在 while和do之间可以放几个命令。命令通常用作测试条
件。
只有当命令的退出状态为 0时,do和done之间命令才被执行,如果退出状态不是 0,则循
环终止。
命令执行完毕,控制返回循环顶部,从头开始直至测试条件为假。

实例

#!/bin/bash
# Program:
#       This program will show the use of if
# History:
# 2015/1/8 Alex First release
i=10
while [[ $i -gt 5 ]]
doecho $ii=`expr $i - 1`;
done
exit 0
注意:在Shell中四则运算不能简简单单的加减乘除,应该要写如下的格式: val1=`$val2 - 1`
其中“=”后面用“`”包住表达式,这个符号在Shell中十分有用,是Tab键上面“~”的原来形式。可以用来将很多命令的结果保存到一个变量中去。接着就是运算符了,运算符的左右两边必须是空格,否则会出错。
deyuy/bin/my_shell >> ./while1.sh
10
9
8
7
6
注意:  1.expr命令的用法请参考 http://blog.chinaunix.net/uid-25880122-id-2937521.html2.``的作用是运行``之间的命令,并且将命令运行的结果返回。详细请参考:http://blog.csdn.net/miyatang/article/details/80771233. 更多while实例参考:http://blog.csdn.net/firefoxbug/article/details/7237319

二、until语句

until循环执行一系列命令直至条件为真时停止。 until循环与while循环在处理方式上刚好相反。一般while循环优于until循环,但在某些时候—也只是极少数情况下, until循环更加有用。

//until循环格式为:
until 条件
命令1
. . .
done

条件可为任意测试条件,测试发生在循环末尾,因此循环至少执行一次—请注意这一
点。

until循环中,只要条件不为真,就执行do和done之间的循环命令,或者说,在until循环中,一直执行do和done之间的循环命令,直到条件为真;

–避免生成死循环。

# Program:
#       This program will show the use of until
# History:
# 2015/1/13 Alex First releasesum=0
num=10until test $num -eq 0dosum=`expr $sum + $num`num=`expr $num - 1`done
echo "sum = $sum"exit 0deyuy/bin/my_shell >> sh until.sh
sum = 55

三、for 语句结构

//for循环一般格式为:
for 变量名i n列表
do
命令1
命令2…
done

当变量值在列表里, for循环即执行一次所有命令,使用变量名访问列表中取值。命令可为任何有效的shell命令和语句。
变量名为任何单词。 in列表用法是可选的,如果不用它, for循环使用命令行的位置参数。
in列表可以包含替换、字符串和文件名,列表可以自定义,也可以通过命令返回值生成,下面是一些常用例子。

1) 整数列表

#!/bin/bash
# Program:
#       This program will show the use of for
# History:
# 2015/1/12 First release
# 自定义列表
for loop in 1 2 3 4 5
doecho "loop=$loop"
done
exit 0deyuy/bin/my_shell >> chmod u+x for1.sh
deyuy/bin/my_shell >> ./for1.sh
loop=1
loop=2
loop=3
loop=4
loop=5//还可以通过读取文件内容生成变量列表deyuy/bin/my_shell >> vim num.txt
1 2 3 4 5
6
7
8
#!/bin/bash
# Program:
# This program will show the use of for
# History:
# 2015/1/12 First release
# 以命令返回值作为列表
i=0
for i in `cat num.txt`
do
echo "i=$i"
done
exit 0
deyuy/bin/my_shell >> ./for1.sh
i=1
i=2
i=3
i=4
i=5
i=6
i=7
i=8

2) 字符串列表

#!/bin/bash
# Program:
#       This program will show the use of for
# History:
# 2015/1/12 First release
# 自定义列表:带引号
i=0
for loop in "apple hhhh bbbb eeee"
doi=`expr $i + 1`echo "loop=$loop"echo "count=$i"
done
exit 0deyuy/bin/my_shell >> chmod u+x for4.sh
deyuy/bin/my_shell >> ./for4.sh
loop=apple hhhh bbbb eeee
count=1
*说明:从结果可以看出for循环打印字符串到结束,包括空格,只执行了一次。如果把 in列表改为 for loop in "apple" "hhhh" "bbbb" "eeee"
则输出结果为
loop=apple
count=1
loop=hhhh
count=2
loop=bbbb
count=3
loop=eeee
count=4
#!/bin/bash
# Program:
#       This program will show the use of for
# History:
# 2015/1/12 First release
# 自定义列表,不带引号
i=0
for loop in apple hhhh bbbb eeeedoi=`expr $i + 1`echo "loop=$loop"echo "count=$i" 
doneexit 0deyuy/bin/my_shell >> ./for4.sh
loop=apple
count=1
loop=hhhh
count=2
loop=bbbb
count=3
loop=eeee
count=4

注意:对比两张方式输出的不同可以看出,如果用户想让把空格分割的每个单词都输出要通过第二种方式。

3) 打印当前目录下所有文件

# Program:
#       This program will show the use of for
# History:
# 2015/1/12 First release
for loop in `ls`
doecho "$loop"
done
exit 0deyuy/bin/my_shell >> sh for2.sh
app1
for1.sh
for2.sh
fun1.sh
hello.sh
hh.sh
if.sh
if2.sh
test.sh
test2.sh
var.sh
while1.sh

4) 循环计数

#!/bin/bash
# Program:
#       This program will show the use of for
#       统计当前目录下文件数
# History:
# 2015/1/12 First release
counter=0
for files in *
docounter=`expr $counter + 1`
done
echo "There are $counter files in `pwd` directory."
exit 0deyuy/bin/my_shell >> sh for3.sh
There are 13 files in /home/deyuy/bin/my_shell directory.

5) 使用位置参数

#!/bin/bash
# Program:
#       This program will show the use of for
# History:
# 2015/1/13 release
i=0
for params
doi=`expr $i + 1`echo "You supplied $params as a command line option"echo "count=$i"
doneecho $paramsexit 0deyuy/bin/my_shell >> ./for5.sh p1 p2 p3
You supplied p1 as a command line option
count=1
You supplied p2 as a command line option
count=2
You supplied p3 as a command line option
count=3
p3###下面的脚本包含i n"$ @",结果与上面的脚本相同。
#!/bin/bash
# Program:
#       This program will show the use of for
# History:
# 2015/1/13 release
i=0
for params in "$@"
doi=`expr $i + 1`echo "You supplied $params as a command line option"echo "count=$i"
doneecho $paramsexit 0deyuy/bin/my_shell >> ./for5.sh p1 p2 p3
You supplied p1 as a command line option
count=1
You supplied p2 as a command line option
count=2
You supplied p3 as a command line option
count=3
p3###下面的脚本包含i n"$ *",结果与上面的脚本不同。
#!/bin/bash
# Program:
#       This program will show the use of for
# History:
# 2015/1/13 release
i=0
for params in "$*"
doi=`expr $i + 1`echo "You supplied $params as a command line option"echo "count=$i"
doneecho $paramsexit 0deyuy/bin/my_shell >> ./for5.sh p1 p2 p3
You supplied p1 p2 p3 as a command line option
count=1
p1 p2 p3
说明:关于$@和$*的区别请参考http://www.cnblogs.com/yuexiaxiaoxi/articles/4203609.html

6)循环嵌套

嵌入循环可以将一个f o r循环嵌在另一个f o r循环内:

for 变量名1 in列表1
do
for 变量名2 in 列表2
do
命令1
. . .
done
done

下面脚本即为嵌入for循环,这里有两个列表apps和scripts。第一个包含服务器上应用
的路径,第二个为运行在每个应用上的管理脚本。对列表 apps上的每一个应用,列表
scripts里的脚本将被运行,脚本实际上为后台运行。脚本使用tee命令在登录文件上放一条
目,因此输出到屏幕的同时也输出到一个文件。查看输出结果就可以看出嵌入for循环怎样使
用列表scripts以执行列表apps上的处理。

#!/bin/bash
# Program:
#       This program will show the use of for
# History:
# 2015/1/13 release
APPS="/apps/accts /apps/claims /apps/stock /apps/serv"
SCRIPTS="audit.check report.run cleanup"
MY_DATE=`date +%H:%M" on "%d/%m/%Y`
i=0
j=0
#outer loop
for loop in $APPS
doi=`expr $i + 1`
#inner loopfor loop2 in $SCRIPTSdoj=`expr $i + 1`echo "system $loop now running $loop2 at $MY_DATE"echo "inner loop counter=$j"doneecho "outer loop counter=$i"
doneecho $paramsdeyuy/bin/my_shell >> ./for6.sh
system /apps/accts now running audit.check at 20:53 on 12/01/2015
inner loop counter=2
system /apps/accts now running report.run at 20:53 on 12/01/2015
inner loop counter=2
system /apps/accts now running cleanup at 20:53 on 12/01/2015
inner loop counter=2
outer loop counter=1
system /apps/claims now running audit.check at 20:53 on 12/01/2015
inner loop counter=3
system /apps/claims now running report.run at 20:53 on 12/01/2015
inner loop counter=3
system /apps/claims now running cleanup at 20:53 on 12/01/2015
inner loop counter=3
outer loop counter=2
system /apps/stock now running audit.check at 20:53 on 12/01/2015
inner loop counter=4
system /apps/stock now running report.run at 20:53 on 12/01/2015
inner loop counter=4
system /apps/stock now running cleanup at 20:53 on 12/01/2015
inner loop counter=4
outer loop counter=3
system /apps/serv now running audit.check at 20:53 on 12/01/2015
inner loop counter=5
system /apps/serv now running report.run at 20:53 on 12/01/2015
inner loop counter=5
system /apps/serv now running cleanup at 20:53 on 12/01/2015
inner loop counter=5
outer loop counter=4

四、case结构

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

case expression inpattern1 )statements ;;pattern2 )statements ;;...
esac

case工作方式如上所示。取值后面必须为单词 i n,每一模式必须以右括号结束。取值可以
为变量或常数。匹配发现取值符合某一模式后,其间所有命令开始执行直至;;。
取值将检测匹配的每一个模式。一旦模式匹配,则执行完匹配模式相应命令后不再继续
其他模式。如果无一匹配模式,使用星号 *捕获该值,再接受其他输入。
模式部分可能包括元字符,与在命令行文件扩展名例子中使用过的匹配模式类型相同,
即:
* 任意字符。
? 任意单字符。
[..] 类或范围中任意字符。

注意:

1.模式字符串中可以使用通配符

2.如果一个模式字符串中包含多个模式,那么各模式之间应以竖线(|)隔开,表各模式是“或”的关系,即只要给定字符串与其中一个模式相配,就会执行其后的命令列表。

3.各模式字符串应是唯一的,不应重复出现,并且要合理安排它们的出现顺序,例如,不应将“*”作为头一个模式字符串,因为“*”可以与任何字符串匹配,若第一个出现,就不会再检查其他模式了。

4.case语句以关键字case开头,以关键字esac结束。

5.case的退出(返回)值是整个结构中最后执行的命令的退出值。若没有执行任何命令,则退出值为0.

#!/bin/bash
# Program:
#       This program will show the use of for
# History:
# 2015/1/13 First release
case $1 in
y|Y)echo "your choice is yes";;
n|N)echo "your choice is no";;
*)echo "your choice is others";;
esac
exit 0deyuy/bin/my_shell >> chmod u+x case.sh
deyuy/bin/my_shell >> ./case.sh y
your choice is yes
deyuy/bin/my_shell >> ./case.sh n
your choice is no
deyuy/bin/my_shell >> ./case.sh jjjj
your choice is others

五、Select结构

格式:

select 变量 in 列表
do
命令行(通常用到循环变量)
done

制作一个选择表,在列表中选择一个选项执行命令行。如果选择的变量不在列表序列中,则返回一个空值。需要用break退出循环。

#!/bin/bash
# Program:
#       This program will show the use of for
# History:
# 2015/1/13 First release
echo "a is 5 ,b is 3. Please select your method: "
a=5
b=3
select var in "a+b" "a-b" "a*b" "a/b"
dobreak
done
case $var in
"a+b")echo 'a+b= '`expr $a + $b`;;
"a-b")echo 'a-b= '`expr $a - $b`;;
"a*b")echo 'a*b= '`expr $a \* $b`;;
"a/b")echo 'a/b= '`expr $a / $b`;;
*)echo "input error"
esacdeyuy/bin/my_shell >> chmod u+x select.sh
deyuy/bin/my_shell >> ./select.sh
a is 5 ,b is 3. Please select your method:
1) a+b
2) a-b
3) a*b
4) a/b
#? 3
a*b= 15

六、break和continue

–1、break:用于立即终止当前循环的执行,break命令可以使用户从循环体中退出来。

–语法:break[n] ,其中,n表示要跳出几层循环,默认值为1

#!/bin/bash
for var1 in 1 2 3
dofor var2 in 0 5doif [ $var1 -eq 2 -a $var2 -eq 0 ]thenbreak 2elseecho "$var1 $var2"fidone
done

运行结果:

1 0

1 5

–2、continue:跳过循环体中在其之后的语句,会返回到本循环层的开头,进行下一次循环。

–语法:continue[n],其中,n表示从包含continue语句的最内层循环体向外跳到第几层循环,默认值为1,循环层数是由内向外编号。

#!/bin/bash
NUMS="1 2 3 4 5 6 7"
for NUM in $NUMS
doQ=`expr $NUM % 2`if [ $Q -eq 0 ]thenecho "Number is an even number!!"continuefiecho "Found odd number"
done

运行结果:

Found odd number
Number is an even number!!
Found odd number
Number is an even number!!
Found odd number
Number is an even number!!
Found odd number

转载于:https://my.oschina.net/u/2391658/blog/708151

shell编程中for/while/util/case/select/break/continue相关推荐

  1. shell 不等于_关于shell编程中的整数值比较的两种方式的简单操作实例

    谈一谈关于shell编程中的整数值比较的两种方式 Shell编程有时处理一个对象时,需要我们对对象进行测试. 只有符合要求的才采取下一步操作,这样做的好处可以避免程序出错. 这个测试的对象可以是文件. ...

  2. linux shell let命令,shell编程中的let与(())

    let与(()) 在shell编程中是可以互换的:它们在循环语句中控制变量变化非常有用: 使用let语句或者(())我们可以像C语言那样写程序~ 对于变量赋值,判断什么的不用繁琐的$VAR, -eq等 ...

  3. shell编程中如何执行oracle语句

    shell编程中如果向oracle中插入数据之类的,需要先把执行语句放到文件中,然后再@这个文件执行 有如下俩种方式供参考: SQL=`sqlplus user/pwd@orains << ...

  4. shell编程中特殊字符的问题总结

    shell编程中特殊字符的问题总结 --同事王怡春的总结: 近日在编写shell脚本的遇到的一些问题,然后上网搜搜学习后,以下是总结后的版本,给大家分享,如有问题,错误,欢迎指正 一 通配符( * ) ...

  5. 掌握shell编程中数组的常见用法及示例

    From: http://www.embeddedlinux.org.cn/html/jishuzixun/201211/19-2386.html 给大家分享下数组的用法小例子,希望能给大家一点帮助. ...

  6. ll文件显示为?????_关于shell编程中的文件测试简单的操作实例

    谈一谈关于shell编程中的文件测试 Shell编程有时处理一个对象时,需要我们对对象进行测试. 只有符合要求的才采取下一步操作,这样做的好处可以避免程序出错. 这个测试的对象可以是文件.字符串.数字 ...

  7. 轻松掌握shell编程中数组的常见用法及示例

    缘起:在老男孩进行linux培训shell编程教学中,发现不少水平不错的网友及同学对数组仍然很迷糊,下面就给大家分享下数组的用法小例子,希望能给大家一点帮助.其实SHELL的数组很简单,好用.我们学习 ...

  8. shell 编程中空格的使用,双引号,单引号,反引号

    http://blog.csdn.net/panda19881/article/details/6626727 1.定义变量时, =号的两边不可以留空格. eg: gender=femal----ri ...

  9. Shell编程中的“局部变量”和“导出变量” (export 用法)

    本原创文章属于<Linux大棚>博客,博客地址为http://roclinux.cn.文章作者为rocrocket. 如果你对子Shell的概念不甚了解,如果你对export的用法还没吃透 ...

最新文章

  1. 怎么选择网管型和非网管型交换机
  2. [C++学习笔记]C++常见问题大全(一)
  3. 如何给python装库_python一键式装单机和RAC数据库
  4. GoldenGate 12.3 MA架构介绍系列(4)–Restful API介绍
  5. hadoop文件的序列化
  6. 远程桌面无法连接方案
  7. 计算机机房维护与管理,计算机机房的管理与维护.doc
  8. oracle中字符串连接
  9. 笔记本怎么打开html的面板,联想笔记本控制面板在哪里打开
  10. ps滑动鼠标放大缩小
  11. D. Three Religions
  12. 使用ASF在Ubuntu下实现Steam云挂卡
  13. 嵌入式linux shadow文件,04.嵌入式Linux文件系统
  14. 职位名称: Java技术经理
  15. java网课|Map线程
  16. 《机器学习实战》(八)-- 树回归
  17. python将一个字符串反转并输出_逆转字符串—输入一个字符串,将其逆转并输出。...
  18. 区块链在物联网中的应用
  19. 7个你绝对没用过的超强搜索引擎
  20. XMLViewer xml查看器

热门文章

  1. socket udp
  2. (原創) 如何解決移除DSP Builder後,在Matlab殘留錯誤訊息的問題? (SOC) (DSP Builder) (Matlab)...
  3. 关于 top、left 结合 translate 实现居中的原理探讨
  4. 关于cookie的文章(cookie与session机制)
  5. 使用Native API 创建进程
  6. TMG 日志队列(Log Queue,扩展名为 .LLQ)持续增长或 TMG
  7. ITIL应用系列之服务台
  8. Shell编程基础---函数、数组
  9. Maven---学习心得---maven的配置文件settings.xml
  10. 【分享】博客美化(6)为你的博文自动添加目录【转】