|
Linux程式设计-11.Shell Script(bash)--(5)控制圈for |
|
![]() |
|
|
|
示了几个简单的Shell Script,相信您应该对Shell Script有点概念了。现在我们开始来仔细研究一些较高等的Shell Script写作。一些进一步的说明,例如"$"、 ">"、"<"、">>"、"1>"、"2>"符号的使用,会在稍後解释。
--------------------------------------------------------------------------------
for name [ in word; ] do list ; done 控制圈。 word是一序列的字,for会将word中的个别字展开,然後设定到name上面。list是一序列的工作。如果[in word;]省略掉,那麽name将会被设定为Script後面所加的参数。
--------------------------------------------------------------------------------
例一: #!/bin/sh
for i in a b c d e f ; do echo $i done
它将会显示出a到f。
--------------------------------------------------------------------------------
例二: 另一种用法,A-Z #!/bin/sh WORD="a b c d e f g h i j l m n o p q r s t u v w x y z"
for i in $WORD ; do echo $i done
这个Script将会显示a到z。
--------------------------------------------------------------------------------
例三 : 修改副档名 如果您有许多的.txt档想要改名成.doc档,您不需要一个一个来。 #!/bin/sh
FILES=`ls /txt/*.txt`
for txt in $FILES ; do doc=`echo $txt | sed "s/.txt/.doc/"` mv $txt $doc done
这样可以将*.txt档修改成*.doc档。
--------------------------------------------------------------------------------
例四 : meow #!/bin/sh # Filename : meow for i ; do cat $i done
当您输入"meow file1 file2 ..."时,其作用就跟"cat file1 file2 ..."一样。
--------------------------------------------------------------------------------
例五 : listbin #!/bin/sh # Filename : listbin
for i in /bin/* ; do echo $i done
当您输入"listbin"时,其作用就跟"ls /bin/*"一样。
--------------------------------------------------------------------------------
例六 : /etc/rc.d/rc 拿一个实际的例来说,Red Hat的/etc/rc.d/rc的启动程式中的一个片断。
for i in /etc/rc.d/rc$runlevel.d/S*; do # Check if the script is there. [ ! -f $i ] && continue
# Check if the subsystem is already up. subsys=${i#/etc/rc.d/rc$runlevel.d/S??} [ -f /var/lock/subsys/$subsys ] || \ [ -f /var/lock/subsys/${subsys}.init ] && continue
# Bring the subsystem up. $i start done
这个例中,它找出/etc/rc.d/rcX.d/S*所有档案,检查它是否存在,然後一一执行。
|
上一篇: Linux程式设计-11.Shell Script(bash)--(4)档案系统检查 下一篇: Linux程式设计-11.Shell Script(bash)--(6)流程控制case
![](/images/xg.gif) |
相关文档 |
|
![](/images/xg.gif) |
发表评论 |
|
|
|
|