認識while循環
用法一、死循環,:在while循環裏面表示永久爲真
[root@localhost test]# cat 7.sh
#!/bin/bash
while :
do
date +%T
sleep 5
done
用法二、順序輸出1-10
[root@localhost test]# cat 8.sh
#!/bin/bash
n=1
while [ $n -le 10 ]
do
echo $n
n=$[$n+1]
done
用法三、檢測用戶輸入
[root@localhost test]# cat 9.sh
#!/bin/bash
while :
do
read -p "Please input a number: " m
n=`echo $m | sed 's/[0-9]//g'`
if [ -z "$n" ]
then
echo $m
exit
fi
done
用法四、檢測用戶輸入
[root@localhost test]# cat 9.sh
#!/bin/bash
n=1
while [ ! -z "$n" ]
do
read -p "Please input a number: " m
n=`echo $m | sed 's/[0-9]//g'`
echo $m
done