while循环,语法如下:
while command
do
Statement(s) to be executed if command is true
done
command可以是一个判断,也可以是一个命令,如读取文件等。
当command条件为真,则执行循环中的语句块,否则直接退出循环。
实例1、简单的while循环
打印从0到10的数据:
pg while1.sh
#!/bin/ksh
i=0
while [ $i -le 10 ]
do
echo $i
i=`expr $i + 1`
done
#EOF
执行脚本:
sh while1.sh
0
1
2
3
4
5
6
7
8
9
10
实例2、我想将当前目录下文件名包含if的文件全部移动到iftest文件夹下面。
这里借助于一个临时文件temp.txt,创建之后,保存从当前文件夹下中查询到的文件;
用完之后,将其删除。
pg mvfile.sh
#!/bin/ksh
echo "moving all file which contain word if to directory iftest"
# create temporary file temp.txt
if [ -f temp.txt ]; then
"&