while the five kinds of conditions
1. mathematical comparison
1 read -p "Num :" num1 2 while [ $num1 -gt 0 ] 3 do 4 echo 'greater' 5 sleep 1 6 7 done
2. string comparison
1 read -p "enter the password:" pw 2 while [ $pw != password ] 3 do 4 read -p "enter the password:" pw 5 6 done 7 echo 'welcome'
3. detect whether a file exists
1 while [ ! -e /tmp/testfolder ] 2 do 3 echo '/tmp/testfolder not exist' 4 sleep 1 5 done
4. Analyzing the plurality of conditions
1 read -p "money:" money 2 read -p "car:" car 3 read -p "house:" house 4 5 while [ $money -lt 100000 ] || [ $car -lt 1 ] || [ $house -lt 1 ] 6 do 7 echo 'no way!' 8 read -p "money:" money 9 read -p "car:" car 10 read -p "house:" house 11 done 12 echo 'Ok!'
5. Analyzing Assignment
1 read -p "char:" ch 2 3 while [ $ch != 'q' ] 4 do 5 echo "char is : $ch" 6 read -p "char:" ch 7 done 8 echo 'you just press q'
6. The wording is similar for
1 i=1 2 while [ $i -lt 10 ] 3 do 4 echo $i 5 i=$((i+1)) 6 done
7.while also continue using the break when the need to pay attention to the order of accumulation
. 1 I = . 1 2 the while [$ I -LT- 10 ] . 3 do . 4 echo $ I . 5 I = $ ((I + . 1 )) . 6 IF [$ I -eq . 5 ]; the then . 7 Continue . 8 Fi . 9 #i = $ ( (I + . 1 )) when the first accumulation continue, when the accumulated BREAK 10 DONE
8. The multiplication table
. 1 n-= . 1 2 the while [-LT-n-$ 10 ] . 3 do . 4 for ((m = . 1 ; m <= $ n; m ++ )) # where m <= $ n written if m <pattern 10 will output very different . 5 do . 6 echo -n -e " $ $ n-MX = $ ((n-m *)) \ T " . 7 DONE . 8 echo . 9 n-= $ ((n-+ . 1 )) 10 DONE
If the above for double while that is replaced while nesting
1 n=1 2 while [ $n -lt 10 ] 3 do 4 #for (( m=1;m<=$n;m++ )) 5 # do 6 m=1 7 while [ $m -lt $n ];do 8 9 echo -n -e "$m x $n = $((n*m))\t" 10 11 m=$(($m+1)) 12 done 13 echo 14 n=$((n+1)) 15 done
9. Read the contents of the passwd
1 while read i;do 2 echo $i 3 done < /etc/passwd
10. Read the contents of the passwd fifth column shows only the first row in the third column
1 IFS=$":" 2 while read f1 f2 f3 f4 f5 f6 f7;do 3 echo "$f1 $f3 $f5" 4 5 done < /etc/passwd
11.until
The difference is that until and while the condition is false until it is executed
Print 10-20
1 i=10 2 until [ $i -gt 20 ];do 3 echo $i 4 i=$((i+1)) 5 done