shell while 循環

認識while循環bash

用法1、死循環,:在while循環裏面表示永久爲真ide

[root@localhost test]# cat 7.shinput

#!/bin/bashit

while :class

dotest

date +%Tsed

sleep 5date

done循環

用法2、順序輸出1-10di

[root@localhost test]# cat 8.sh

#!/bin/bash

n=1

while [ $n -le 10 ]

do

echo $n

n=$[$n+1]

done

用法3、檢測用戶輸入

[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

用法4、檢測用戶輸入

[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

相關文章
相關標籤/搜索