until循环
条件中1为真,0为假
until循环执行一系列命令直至条件为真时停止。 until循环与while循环在处理方式上刚好相反。一般while循环优于until循环,但在某些时候—也只是极少数情况下,until循环更加有用。
until循环格式为:
- until 条件
- do
- 命令1
- . . .
- done
until 条件
do
命令1
. . .
done
条件可为任意测试条件,测试发生在循环末尾,因此循环至少执行一次—请注意这一点
监视文件
下面例子中,until循环不断挂起做睡眠,直至文件process.LCK 被删除。文件删除后,脚本进入正常处理过程。
- /home/l/g/tomotoboy/until >ls
- until_lck untilwho
- /home/l/g/tomotoboy/until >[ -f untilwho ]
- /home/l/g/tomotoboy/until >echo $?
- 0
- /home/l/g/tomotoboy/until >[ ! -f untilwho ]
- /home/l/g/tomotoboy/until >echo $?
- 1
- /home/l/g/tomotoboy/until >rm untilwho
- /home/l/g/tomotoboy/until >[ ! -f untilwho ]
- /home/l/g/tomotoboy/until >echo $?
- 0
-
- #!/bin/sh
- #until_lck
- touch `pwd`/process.LCK
- LOCK_FILE=`pwd`/process.LCK
- until [ ! -f $LOCK_FILE ]
- do
- sleep 1
- done
- echo "file deleted"
/home/l/g/tomotoboy/until >ls
until_lck untilwho
/home/l/g/tomotoboy/until >[ -f untilwho ]
/home/l/g/tomotoboy/until >echo $?
0
/home/l/g/tomotoboy/until >[ ! -f untilwho ]
/home/l/g/tomotoboy/until >echo $?
1
/home/l/g/tomotoboy/until >rm untilwho
/home/l/g/tomotoboy/until >[ ! -f untilwho ]
/home/l/g/tomotoboy/until >echo $?
0
#!/bin/sh
#until_lck
touch `pwd`/process.LCK
LOCK_FILE=`pwd`/process.LCK
until [ ! -f $LOCK_FILE ]
do
sleep 1
done
echo "file deleted"
查找tomotoboy有没有登录,登陆后给自己发个邮件
- /home/l/g/tomotoboy/until >cat untilwho
- #!/bin/sh
- #until_who
- IS_TOMOTOBOY=`who | grep tomotoboy`
- until [ "$IS_TOMOTOBOY" ]
- do
- sleep 5
- done
- echo "Wtch it. tomotoboy in " | mail tomotoboy
/home/l/g/tomotoboy/until >cat untilwho
#!/bin/sh
#until_who
IS_TOMOTOBOY=`who | grep tomotoboy`
until [ "$IS_TOMOTOBOY" ]
do
sleep 5
done
echo "Wtch it. tomotoboy in " | mail tomotoboy
while循环
while循环用于不断执行一系列命令,也用于从输入文件中读取数据,其格式为:
- while 命令
- do
- 命令1
- 命令2
- . . .
- done
while 命令
do
命令1
命令2
. . .
done
虽然通常只使用一个命令,但在 while和do之间可以放几个命令。命令通常用作测试条件。只有当命令的退出状态为 0时,do和done之间命令才被执行,如果退出状态不是 0,则循环终止。
- #!/bin/sh
- #whilecount
- COUNT=0
- while [ $COUNT -lt 5 ]
- do
- echo "$COUNT"
- COUNT=`expr $COUNT + 1`
- done
-
- /home/l/g/tomotoboy >whilecount
- 0
- 1
- 2
- 3
- 4
#!/bin/sh
#whilecount
COUNT=0
while [ $COUNT -lt 5 ]
do
echo "$COUNT"
COUNT=`expr $COUNT + 1`
done
/home/l/g/tomotoboy >whilecount
0
1
2
3
4
读文件
- #!/bin/sh
- #whileread
- while read LINE
- do
- echo "$LINE"
- done < sed.txt
#!/bin/sh
#whileread
while read LINE
do
echo "$LINE"
done < sed.txt
break
break命令允许跳出循环。break通常在进行一些处理后退出循环或 case语句。如果是在一个嵌入循环里,可以指定跳出的循环个数。例如如果在两层循环内,用 break刚好跳出整个循环。
跳出case语句
下面的例子中,脚本进入死循环直至用户输入数字大于 5。要跳出这个循环,返回到shell提示符下,break使用脚本如下:
- #!/bin/sh
- #breakout
- #while : means loop forever
- while :
- do
- echo -n "Enter any number [1..5]: "
- read ANS
- case $ANS in
- 1|2|3|4|5) echo " great you entered a number between 1 and 5"
- ;;
- *)echo "Wrong number..bye"
- break
- ;;
- esac
- done
#!/bin/sh
#breakout
#while : means loop forever
while :
do
echo -n "Enter any number [1..5]: "
read ANS
case $ANS in
1|2|3|4|5) echo " great you entered a number between 1 and 5"
;;
*)echo "Wrong number..bye"
break
;;
esac
done
- /home/l/g/tomotoboy >breakout
- -n Enter any number [1..5]:
- 3
- great you entered a number between 1 and 5
- -n Enter any number [1..5]:
- 6
- Wrong number..bye
/home/l/g/tomotoboy >breakout
-n Enter any number [1..5]:
3
great you entered a number between 1 and 5
-n Enter any number [1..5]:
6
Wrong number..bye
continue
continue命令类似于break命令,只有一点重要差别,它不会跳出循环,只是跳过这个循环步。