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*所有档案,检查它是否存在,然後一一执行。 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值