shell while 循環

認識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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章