Linux程式设计-11.ShellScript(bash)--(5)控制圈for
ReadyGo!技术成就梦想
网络搜索
efish
2008-2-24 2:50:11
示了几个简单的ShellScript,相信您应该对ShellScript有点概念了。现在我们开始来仔细研究一些较高等的ShellScript写作。一些进一步的说明,例如"$"、">"、"<"、">>"、"1>"、"2>"符号的使用,会在稍後解释。
--------------------------------------------------------------------------------
fornamedolist;done
控制圈。
word是一序列的字,for会将word中的个别字展开,然後设定到name上面。list是一序列的工作。如果省略掉,那麽name将会被设定为Script後面所加的参数。
--------------------------------------------------------------------------------
例一:
#!/bin/sh
foriinabcdef;do
echo$i
done
它将会显示出a到f。
--------------------------------------------------------------------------------
例二:另一种用法,A-Z
#!/bin/sh
WORD="abcdefghijlmnopqrstuvwxyz"
foriin$WORD;do
echo$i
done
这个Script将会显示a到z。
--------------------------------------------------------------------------------
例三:修改副档名
如果您有许多的.txt档想要改名成.doc档,您不需要一个一个来。
#!/bin/sh
FILES=`ls/txt/*.txt`
fortxtin$FILES;do
doc=`echo$txt|sed"s/.txt/.doc/"`
mv$txt$doc
done
这样可以将*.txt档修改成*.doc档。
--------------------------------------------------------------------------------
例四:meow
#!/bin/sh
#Filename:meow
fori;do
cat$i
done
当您输入"meowfile1file2..."时,其作用就跟"catfile1file2..."一样。
--------------------------------------------------------------------------------
例五:listbin
#!/bin/sh
#Filename:listbin
foriin/bin/*;do
echo$i
done
当您输入"listbin"时,其作用就跟"ls/bin/*"一样。
--------------------------------------------------------------------------------
例六:/etc/rc.d/rc
拿一个实际的例来说,RedHat的/etc/rc.d/rc的启动程式中的一个片断。
foriin/etc/rc.d/rc$runlevel.d/S*;do
#Checkifthescriptisthere.
&&continue
#Checkifthesubsystemisalreadyup.
subsys=${i#/etc/rc.d/rc$runlevel.d/S??}
||\
&&continue
#Bringthesubsystemup.
$istart
done
这个例中,它找出/etc/rc.d/rcX.d/S*所有档案,检查它是否存在,然後一一执行。
-
相关文章
Linux程式设计-11.ShellScript(bash)--(6)流程控制c…
Linux程式设计-11.ShellScript(bash)--(6)流程控制case
Linux程式设计-11.ShellScript(bash)--(7)流程控制s…
Linux程式设计-11.ShellScript(bash)--(7)流程控制select
Linux程式设计-11.ShellScript(bash)--(8)返回状态E…
Linux程式设计-11.ShellScript(bash)--(8)返回状态Exit
Linux程式设计-11.ShellScript(bash)--(9)流程控制if
Linux程式设计-11.ShellScript(bash)--(9)流程控制if
Linux程式设计-11.ShellScript(bash)--(10)控制圈wh…
Linux程式设计-11.ShellScript(bash)--(10)控制圈while/until
Linux程式设计-11.ShellScript(bash)--(11)参数与变数
Linux程式设计-11.ShellScript(bash)--(11)参数与变数
Linux程式设计-11.ShellScript(bash)--(13)Bash内建…
Linux程式设计-11.ShellScript(bash)--(13)Bash内建指令集
Linux程式设计-11.ShellScript(bash)--(12)函数func…
Linux程式设计-11.ShellScript(bash)--(12)函数function
Linux程式设计-11.ShellScript(bash)--(14)Bash内建…
Linux程式设计-11.ShellScript(bash)--(14)Bash内建参数
Linux程式设计-11.ShellScript(bash)--(15)提示符号
Linux程式设计-11.ShellScript(bash)--(15)提示符号
2秒记住本站域名
玩过泡泡龙吗?Readygo?Go! 再加上.Com.Cn的后缀,那就是大名小顶的ReadyGo.com.cn
