江苏赛华建设监理有限公司网站,宁夏住房城乡建设厅网站,网站开发证书要求,盘锦如何做百度的网站文章目录 shell循环语句for命令**读取列表中的值****读取列表中的复杂值****从变量读取列表**迭代数组**从命令读取值****用通配符读取目录**C语言风格的shell for循环 shell循环while命令shell 循环的until命令shell循环跳出的break/continue命令break命令continue命令trick 欢… 文章目录 shell循环语句for命令**读取列表中的值****读取列表中的复杂值****从变量读取列表**迭代数组**从命令读取值****用通配符读取目录**C语言风格的shell for循环 shell循环while命令shell 循环的until命令shell循环跳出的break/continue命令break命令continue命令trick 欢迎访问个人网络日志知行空间 shell循环语句for命令
bash shell中 for 命令的基本格式:
for var in list
docommands
donelist参数中需要提供迭代中要用到的一系列值。指定列表中值的方法
读取列表中的值
#!/bin/bash
for v in a b c; doecho The Text is $v
done# The Text is a
# The Text is b
# The Text is c读取列表中的复杂值
当列表中包含单引号时会导致迭代出错
#!/bin/bash
for v in I cant argee thats right ; doecho The word is $v
done# The word is I
# The word is cant argee thats
# The word is right
可以看到因为受单引号的影响并没有按word by word输出。解决办法有两个
对单引号进行转义使用双引号包围
#!/bin/bash
for v in I can\t argee thats right ; doecho The word is $v
done# The word is I
# The word is cant
# The word is argee
# The word is thats
# The word is right
for 命令用空格来划分列表中的每个值。如果在单独的数据值中有空格就必须用双引号将这些值圈起来。
#!/bin/bash
for v in a b c ; doecho The word is $v
done# The word is a
# The word is b c从变量读取列表
将一系列值都集中存储在了一个变量中然后需要遍历变量中的整个列表。
#!/bin/bash
lla b c
ll$ll dd
for s in $ll
doecho char $s
done # char a
# char b
# char c
# char dd迭代数组
前面介绍过数组的定义这里使用for循环在bash shell中使用array的方式如下
#!/bin/basharr(1 2 3)
for i in ${arr[]}; doecho $i
done# 1
# 2
# 3${array[]}是取数组中的所有值。
从命令读取值
生成列表中所需值的另外一个途径就是使用命令的输出。
#!/bin/bash# content of file
# a bbb
# b
# cc
# dddfilefile
for v in $(cat $file)
doecho value from file $v
done# value from file a
# value from file bbb
# value from file b
# value from file cc
# value from file ddd
设置for命令分割符bash shell中默认的分割符是空格/制表符/换行符这是由特殊的环境变量 IFS(internal field separator),也被称为内部字段分隔符。
要解决这个问题可以在shell脚本中临时更改 IFS 环境变量的值来限制被bash shell当作字段分隔符的字符。例如如果修改 IFS 的值使其只能识别换行符那就必须这么做
IFS$\n#!/bin/bash# content of file
# a bbb
# b
# cc
# dddIFS$\n
filefile
for v in $(cat $file)
doecho value from file $v
done# value from file a bbb
# value from file b
# value from file cc
# value from file ddd
如果要指定多个 IFS 字符只要将它们在赋值行串起来就行。
IFS$\n:;用通配符读取目录
可以用 for 命令来自动遍历目录中的文件。进行此操作时必须在文件名或路径名中 使用通配符。它会强制shell使用文件扩展匹配。文件扩展匹配是生成匹配指定通配符的文件名或 路径名的过程。
#!/bin/bashfor file in /home/rob/* /home/rich/badtest
doif [ -d $file ]thenecho $file is a directoryelif [ -f $file ]thenecho $file is a fileelseecho $file doesnt existfi
done# /home/rob/模板 is a directory
# /home/rob/file is a file
# /home/rob/test1 is a file
# /home/rob/test2 is a file
# /home/rob/test.sh is a file
# /home/rich/badtest doesnt exist
C语言风格的shell for循环
shell还可以支持类似C语言的for循环变量和等号之间可以有空格访问变量的值不需要$符号。但这会造成shell程序编写人员的困惑减少使用。
#!\bin\bashfor (( i1; i 5; i))
doecho The next number is $i
done# The next number is 1
# The next number is 2
# The next number is 3
# The next number is 4
# The next number is 5
shell循环while命令
while命令允许定义一个要测试的命令然后循环执行一组命令只要定义的测试命令返回的是退出状态码 0 。它会在每次迭代的一开始测试 test 命令。在 test 命令返回非零退出状态码时 while 命令会停止执行那组命令。
while 命令格式
while test command
doother commands
donewhile 命令中定义的 test command 和 if-then 语句中的格式一模一样。
最常见的 test command 的用法是用方括号来检查循环命令中用到的shell变量的值。
#!/bin/bashnu5
while [ $nu -gt 0 ]
doecho nu: $nunu$[ $nu - 1 ]
done# nu: 5
# nu: 4
# nu: 3
# nu: 2
# nu: 1while 命令允许在 while 语句行定义多个测试命令。只有最后一个测试命令的退出状态码 会被用来决定什么时候结束循环。
#!/bin/bashnu5
while echo $nu[ $nu -gt 0 ]
doecho nu: $nunu$[ $nu - 1 ]
done# 5
# nu: 5
# 4
# nu: 4
# 3
# nu: 3
# 2
# nu: 2
# 1
# nu: 1
# 0第一个测试简单地显示了 var1 变量的当前值。第二个测试用方括号来判断 var1 变量的值。 在循环内部 echo 语句会显示一条简单的消息说明循环被执行了。运行本例时输出 以0结束,说明执行了[ $nu -gt 0 ]之前的echo $nu语句。
shell 循环的until命令
while命令中只要定义的测试命令返回的是退出状态码 0while就会一直执行在 test 命令返回非零退出状态码时 while命令会停止执行那组命令。
while test command
doother commands
doneuntil命令和 while 命令工作的方式完全相反。只有测试命令的退出状态码不为 0 bash shell才会执行循环中列出的命令。
until test commands
doother commands
doneuntil命令的一个实例
#!/bin/bash
nu8
until [ $nu -eq 0 ]
do echo $nunu$[ $nu - 2 ]
done# 8
# 6
# 4
# 2和 while 命令类似你可以在 until 命令语句中放入多个测试命令。只有最后一个命令的退 出状态码决定了bash shell是否执行已定义的 other commands 。
#!/bin/bash
nu8
until [ $nu -eq 0 ]echo $nu
doecho $nunu$[ $nu - 2 ]
doneshell会执行指定的多个测试命令只有在最后一个命令成立时停止。
shell循环跳出的break/continue命令
break命令
当满足某个条件时有时会希望终止循环shell和大多数变成语言一样支持break/continue命令其使用方法:
#!/bin/bash
for (( a1; a10; a ))
doecho value a $afor((b1; b10; b ))doecho value b $bif [ $b -eq 4 ]; thenecho breakbreak 2fidone
done# break
# value 1
# value 2
# value 3
# value 4值得注意的是break默认是跳出当前层的循环而与其他大多数语言不同的是shell的break命令还支持break n参数n表示跳出几层循环默认为1。
#!/bin/bash
for (( a1; a10; a ))
doecho value a $afor((b1; b10; b ))doecho value b $bif [ $b -eq 4 ]; thenecho breakbreak 2fidone
done# value a 1
# value b 1
# value b 2
# value b 3
# value b 4
# break如上break会直接结束循环。在while和until循环中同样支持break命令。
continue命令
continue命令可以提前中止某次循环中的命令但并不会完全终止整个循环。
#!/bin/bash
for (( v1; v10; v ))
doif [ $v -gt 5 ] [ $v -lt 8 ]; thencontinuefiecho Iteration: $v
done# Iteration: 1
# Iteration: 2
# Iteration: 3
# Iteration: 4
# Iteration: 5
# Iteration: 8
# Iteration: 9
# Iteration: 10也可以在 while 和 until 循环中使用 continue 命令但要特别小心。记住当shell执行continue 命令时它会跳过剩余的命令。
和 break 命令一样 continue 命令也允许通过命令行参数指定要继续执行哪一级循环continue n。
#!/bin/bash
for (( a 1; a 5; a ))
doecho Iteration $a:for (( b 1; b 3; b ))doif [ $a -gt 2 ] [ $a -lt 4 ]; thencontinue 2fivar3$[ $a * $b ]echo The result of of $a * $b is $var3done
done# Iteration 1:
# The result of of 1 * 1 is 1
# The result of of 1 * 2 is 2
# Iteration 2:
# The result of of 2 * 1 is 2
# The result of of 2 * 2 is 4
# Iteration 3:
# Iteration 4:
# The result of of 4 * 1 is 4
# The result of of 4 * 2 is 8
# Iteration 5:
# The result of of 5 * 1 is 5
# The result of of 5 * 2 is 10
trick
在上面的命令中使用重定向可以将循环中命令的结果保存到文件中。
#!/bin/bash
for (( a 1; a 5; a ))
doecho Iteration $a:for (( b 1; b 3; b ))doif [ $a -gt 2 ] [ $a -lt 4 ]; thencontinue 2fivar3$[ $a * $b ]echo The result of of $a * $b is $var3done
done out.txt还可以使用管道命令将循环的结果传递给别的命令
#!/bin/bashfor (( a 1; a 5; a ))
doecho Iteration $a:for (( b 1; b 3; b ))doif [ $a -gt 2 ] [ $a -lt 4 ]; thencontinue 2fivar3$[ $a * $b ]echo The result of of $a * $b is $var3done
done | sort欢迎访问个人网络日志知行空间