循环可以不停地运行某个程序段,直到使用者配置的条件达成为止。
一般来说,不定循环最常见的就是下面这两种状态了。
while [ condition ] <==中括号内的状态就是判断式
do <==do是循环的开始!
程序段落
done <==done是循环的结束
另外一种不定循环的方式:
until [ condition ]
do
程序段落
done
假设要让用户输入yes或者是YES才结束程序的运行,否则就一直运行并提示用户输入字符。
[root@Server01 scripts]# vim sh13.sh
export PATH
while [ "$yn" != "yes" -a "$yn" != "YES" ]
do
read -p "Please input yes/YES to stop this program: " yn
done
echo "OK! you input the correct answer."
而如果$yn是‘yes’或‘YES’时,就会离开循环”,使用until实现?
[root@Server01 scripts]# vim sh13-2.sh
!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
until [ "$yn" == "yes" -o "$yn" == "YES" ]
do
read -p "Please input yes/YES to stop this program: " yn
done
echo "OK! you input the correct answer."