Linux程式设计-11.Shell Script(bash)--(5)控制圈for
来源:互联网 发布:vmware12虚拟机安装mac 编辑:程序博客网 时间:2024/04/24 18:39
示了几个简单的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*所有档案,检查它是否存在,然後一一执行。
--------------------------------------------------------------------------------
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)--(5)控制圈for
- Linux程式设计-11.Shell Script(bash)--(6)流程控制case
- Linux程式设计-11.Shell Script(bash)--(7)流程控制select
- Linux程式设计-11.Shell Script(bash)--(1)简介
- Linux程式设计-11.Shell Script(bash)--(2)教学例
- Linux程式设计-11.Shell Script(bash)--(3)用於自动备份的Shell Script
- Linux程式设计-11.Shell Script(bash)--(4)档案系统检查
- Linux Bash shell (for循环)
- shell script - BASH
- Shell Script(bash)简介
- bash shell script
- Bash Shell Script Tutorials
- create bash-shell-script
- Linux重新学习--shell script--控制结构
- 11.Linux - Bash shell
- Linux bash shell special characters and script syntax.
- 小白linux bash 学习二 shell Script
- Linux Bash Shell学习(十):流程控制——for
- Web services security (WS-Security)
- [邮件] 中国第一封电子邮件
- 乞讨在中国
- 基于Tomcat+Oracle的应用程序安装配置手册
- 电子公文的七大问题与八大关系(转)
- Linux程式设计-11.Shell Script(bash)--(5)控制圈for
- Linux程式设计-11.Shell Script(bash)--(6)流程控制case
- Linux程式设计-11.Shell Script(bash)--(7)流程控制select
- 今天感冒
- w3243
- 字符集问题的初步探讨(二)
- The Ten Best Vocabulary Learning Tips
- 关于shared pool的深入探讨(六)
- Code Complete, My old friend.