linuxshell命令循环执⾏,shell编程中forwhileuntil循环命令⼀、for命令
在shell编程中,有时我们需要重复执⾏⼀直命令直⾄达到某个特定的条件,bash shell中,提供了for命令,允许你创建⼀个遍历⼀系列值的循环,每次迭代都通过⼀个该系列中的值执⾏⼀组预定义的命令。
for的基本格式:
for var in list
do
commands
done
在list中,你提供了迭代中要⽤的⼀系列值。在每个迭代中,变量var包含列表中的当前值,第⼀个迭代会适⽤列表中的第⼀个值,第⼆个迭代使⽤第⼆个值,以此类推,直⾄列表中的所有值都过⼀遍。
1.1读取列表中的值[root@sh shell]# cat for1.sh
#!/bin/bash
for test in aaa bbb ccc ddd
do
echo the next state is $test
done
[root@sh shell]# sh for1.sh
the next state is aaa
the next state is bbb
the next state is ccc
the next state is ddd
$test变量的值会在shell脚本的最后⼀次迭代中⼀直保持有效,除⾮你修改了它[root@sh shell]# cat for1.sh
#!/bin/bash
for test in aaa bbb ccc ddd
do
echo the next state is $test
done
echo "the last state we visited was $test"
test=fff
echo "wait. now we're visiting $test"
[root@sh shell]# sh for1.sh
the next state is aaa
the next state is bbb
the next state is ccc
the next state is ddd
the last state we visited was ddd
wait. now we're visiting fff
1.2读取列表中的复杂值
在shell脚本中,优势你会遇到难处理的数。下⾯是个给shell脚本程序员带来⿇烦的经典例⼦:[root@sh shell]# cat for2.sh #!/bin/bash
for test in I don't know if this'll work
do
echo "word:$test"
done
[root@sh shell]# sh for2.sh
word:I
word:dont know if thisll
word:work
解决办法:使⽤转义符或者双引号[root@sh shell]# cat for2.sh
#!/bin/bash
for test in I don\'t know if "this'll" work
do
echo "word:$test"
done
[root@sh shell]# sh for2.sh
word:I
word:don't
word:know
word:if
word:this'll
word:work
记住:for循环假定每⼀个值都是⽤空格分割的,如果在单独的数字值中有空格,那么你必须使⽤双引号来将这些值圈起来。
1.3从变量读取列表[root@sh shell]# cat for3.sh
>>>>>###
#!/bin/bash
list="aaa bbb ccc ddd eee"
list=$list" Connecticut"
for state in $list
do
echo "Have you ever visited $state"
done
[root@sh shell]# sh for3.sh
Have you ever visited aaa
Have you ever visited bbb
Have you ever visited ccc
Have you ever visited ddd
Have you ever visited eee
Have you ever visited Connecticut
1.4从命令读取值[root@sh shell]# cat for4.sh
#!/bin/bash
file="/root/shell/states" #如果是在当前不⽤绝对路径,file=“states”即可
for state in `cat $file`
dolinux命令及shell编写
echo "Visit beautiful $state"
done
[root@sh shell]# sh for4.sh
Visit beautiful shanghai
Visit beautiful beijing
Visit beautiful hangzhou
Visit beautiful nanjing
Visit beautiful guangzhou
[root@sh shell]# cat states
shanghai
beijing
hangzhou
nanjing
guangzhou
1.5更改字段分隔符空格;
制表符:
换⾏符
如果bash shell在数据中看到了这些字符中任意⼀个,它就会假定你在列表中开始了⼀个新的数据段。
要解决这个问题,你可以在你shell脚本中临时更改IFS环境变量的值来限制⼀下被bash shell当作字段分隔符的字符。但这种⽅式有点奇怪,⽐如,如果你⾏IFS的值使其只能识别换⾏符,你必须这么做:
IFS=$'\n'
将这个语句加⼊到脚本中,告诉bash shell在数据值中忽略空格和制表符。[root@sh shell]# cat for5.sh
#!/bin/bash
file="states"
IFS=$'\n'
for state in `cat $file`
do
echo "Visit beautiful $state"
done
[root@sh shell]# sh for5.sh
Visit beautiful shanghai
Visit beautiful beijing
Visit beautiful hangzhou
Visit beautiful nanjing
Visit beautiful guang zhou
Visit beautiful nan ning
Visit beautiful jiang nan
在处理长脚本中,可能在⼀个地⽅需要修改IFS的值,然后忘掉它在脚本中其他地⽅还原默认值。
例如:
IFS.OLD=$IFS
IFS=$'\n'
IFS=$IFS.OLD
其他的IFS值,如:在/etc/passwd中可能⽤到
IFS=:
也可以赋值多个IFS:
IFS=$'\n:;"'
1.6⽤通配符读取⽬录[root@sh shell]# cat for6.sh
#!/bin/bash
for file in /home/*
do
if [ -d "$file" ];then
echo "$file is a directory"
elif [ -f "$file" ];then
echo "$file is a file"
fi
done
[root@sh shell]# sh for6.sh
/home/apache-tomcat-8.0. is a file
/home/dir1 is a directory
/home/dir2 is a directory
/home/fie1 is a file
/home/fie2 is a file
/
home/fie22 is a file[root@sh shell]# cat for7.sh
#!/bin/bash
for file in /home/* /home/badtest
do
if [ -d "$file" ];then
echo "$file is a directory"
elif [ -f "$file" ];then
echo "$file is a file"
else
echo "$file doesn't exist"
fi
done
[root@sh shell]# sh for7.sh
/home/apache-tomcat-8.0. is a file
/home/dir1 is a directory
/home/dir2 is a directory
/home/fie1 is a file
/home/fie2 is a file
/home/fie22 is a file
/home/badtest doesn't exist
⼆、C语⾔风格的for命令
2.1 C语⾔风格的for命令
C语⾔的for命令有⼀个⽤来指明变量的特殊⽅法、⼀个必须保持成⽴才能继续迭代的条件,以及另⼀个为每个迭代改变变量的⽅法。当指定的条件不成⽴时,for循环就会停⽌。条件等式通过标准的数字符号定义。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
linux下执行shell命令方法简介
« 上一篇
常用的linux shell命令
下一篇 »
发表评论