for循环
for 变量名 in 列表
do
命令 1
命令 2
done
当变量值在列表里,for循环即执行一次所有命令,使用变量名访问列表中取值。命令可为任何有效的shell命令和语句。变量名为任何单词。in列表用法是可选的,如果不用它, for循环使用命令行的位置参数。in列表可以包含替换、字符串和文件名,下面看一些例子。
- /home/l/g/tomotoboy/for >cat fortest
- #!/bin/sh
- #for_i
- for loop in 1 2 3 4 5 6
- do
- echo $loop
- done
-
- #forlist
- for loop in "orange red blue grey"
- do
- echo $loop
- done
-
- #forls
- for loop in `ls`
- do
- echo $loop
- done
-
-
- #forparam
- for params
- do
- echo "you supplied $params as command line option"
- echo $params
- done
-
- for params in "$@"
- do
- echo "you supplied $params as command line option"
- echo $params
- done
-
- #forcommand
- for loop
- do
- find `pwd` -name $loop -print
- done
-
-
- /home/l/g/tomotoboy/for >fortest fortest
- 1
- 2
- 3
- 4
- 5
- 6
- orange red blue grey
- fortest
- you supplied fortest as command line option
- fortest
- you supplied fortest as command line option
- fortest
/home/l/g/tomotoboy/for >cat fortest
#!/bin/sh
#for_i
for loop in 1 2 3 4 5 6
do
echo $loop
done
#forlist
for loop in "orange red blue grey"
do
echo $loop
done
#forls
for loop in `ls`
do
echo $loop
done
#forparam
for params
do
echo "you supplied $params as command line option"
echo $params
done
for params in "$@"
do
echo "you supplied $params as command line option"
echo $params
done
#forcommand
for loop
do
find `pwd` -name $loop -print
done
/home/l/g/tomotoboy/for >fortest fortest
1
2
3
4
5
6
orange red blue grey
fortest
you supplied fortest as command line option
fortest
you supplied fortest as command line option
fortest
使用for循环连接服务器
因为for循环可以处理列表中的取值,现设变量为网络服务器名称,并使用for循环连接每一服务器。
- #!/bin/sh
- #forping
- HOSTS="sina.com cqu.edu.cn baidu.com"
- for loop in $HOSTS
- do
- ping -c 2 $loop
- done
#!/bin/sh
#forping
HOSTS="sina.com cqu.edu.cn baidu.com"
for loop in $HOSTS
do
ping -c 2 $loop
done
多文件转换
匹配所有以.sed结尾文件并将其转换为大写。这里使用了 l s和cat命令。l s用于查询出相关文件,cat用于将之管道输出至trr命令。目标文件扩展名为.UC,注意在for循环中使用l s命令时反引号的用法。
- #!/bin/sh
- #forstr
- for files in `ls *.sed`
- do
- cat $files |tr "[a-z]" "[A-Z]" >$files.UC
- done
-
- /home/l/g/tomotoboy >cat change.sed
- #!/bin/sed -f
- #change.sed
- /tomotoboy/ c\
- I love China,Ilove Wanzhi.
- /home/l/g/tomotoboy >ls *.UC
- append.sed.UC change.sed.UC
- /home/l/g/tomotoboy >cat change.sed.UC
- #!/BIN/SED -F
- #CHANGE.SED
- /TOMOTOBOY/ C\
- I LOVE CHINA,ILOVE WANZHI.
#!/bin/sh
#forstr
for files in `ls *.sed`
do
cat $files |tr "[a-z]" "[A-Z]" >$files.UC
done
/home/l/g/tomotoboy >cat change.sed
#!/bin/sed -f
#change.sed
/tomotoboy/ c\
I love China,Ilove Wanzhi.
/home/l/g/tomotoboy >ls *.UC
append.sed.UC change.sed.UC
/home/l/g/tomotoboy >cat change.sed.UC
#!/BIN/SED -F
#CHANGE.SED
/TOMOTOBOY/ C\
I LOVE CHINA,ILOVE WANZHI.
多sed删除操作
下面的例子中,sed用于删除所有空文件,并将输出导至以 . HOLD.为扩展名的新文件,mv将这些文件移至初始文件中。
- #!/bin/sh
- #forsed
- for files in `ls *.sed`
- do
- sed -e "/^$/d" $files > $files.HOLD
- mv $files.HOLD $files
- done
#!/bin/sh
#forsed
for files in `ls *.sed`
do
sed -e "/^$/d" $files > $files.HOLD
mv $files.HOLD $files
done
循环计数
前面讨论expr时指出,循环时如果要加入计数,使用此命令。下面使用 l s在for循环中列出文件及其数目。
- /home/l/g/tomotoboy >cat forcount
- #!/bin/sh
- #forcount
- count=0
- for files in *
- do
- count=`expr $count + 1`
- done
- echo "There are $count files in `pwd` we need to process"
/home/l/g/tomotoboy >cat forcount
#!/bin/sh
#forcount
count=0
for files in *
do
count=`expr $count + 1`
done
echo "There are $count files in `pwd` we need to process"