The format of the while loop:
while expression Do Commandcommand 、、、 Done
1. Counter-controlled while loop:
Used primarily for the number of data and strings that have been accurately known to be entered.
Example:
#!/bin/bashint=1 while (($Int. <=5 )) do echo $int"int++" Done
2. End tag Control while loop
Mainly used to not know the number of read data, but you can set a special data value to end the loop, the special value is called the end tag, by prompting the user input to operate.
Example:
#!/bin/bash# using the end tag to control the while loop implementation guess 1~number within 10Echo "Please input the num (1~~10):"Read num while[[$num! =4 ]] Doif[$num-lt4]; Then Echo "Too small,try again."Read numelif[$num-GT4]; Then Echo "Too big,try again."Read numElseExit0fi DoneEcho "yes,you is right!"
3. Flag-controlled while loop
User input flag value to control the end of the loop
Example:
#!/bin/BashEcho "Please input the num:"Read numsum=0I=1Signal=0# while[[$signal! =1 ]] while($signal! =1)) Doif[$i-eq $num]; Then Let"signal=1" Let"sum+=i" Echo "1+2 、、、 + $num = $sum"Else Let"Sum=sum+i" Let"i++"fi Done
4. Command-line controlled while loop
Example:
#!/bin/bashecho"echo " while"" ]"doesecho $1shift done
Shell's While loop