Turn from: Http://blog.chinaunix.net/uid-25880122-id-2901409.htmlwhile loop format
- While expression
- Do
- Command
- Command
- ```
- Done
1. The counter-controlled while loop is used primarily for the number of data and strings that have been accurately known to be entered. Example
- 1 #!/bin/sh
- 2 Int=1
- 3 while (($int <=5))
- 4 Do
- 5 echo $int
- 6 Let "int++"
- 7 Done
2, end tag control while loop is 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
- 1 #用脚本演示使用结束标记控制while循环实现猜1 the number in the ~
- 2 #!/bin/sh
- 3
- 4 echo "Please input the num (1~~10):"
- 5 Read num
- 6 while [[$num! = 4]]
- 7 do
- 8 If [$num-lt 4]
- 9 Then
- echo "Too small, Try again ..."
- Read num
- elif [$num-GT 4]
- Then
- echo "Too big, Try again ..."
- Read num
- + Else
- Exit 0
- Fi
- Done
- echo "Yes, you is right!"
3, Peugeot control while loop user input flag value to control the Loop end example
- 1 #!/bin/sh
- 2 echo "Please input the num:"
- 3 Read num
- 4 sum=0
- 5 I=1
- 6 signal=0
- 7 while [[$signal! = 1]]
- 8 Do
- 9 If [$i-eq $num]
- Ten Then
- One-let "signal=1"
- "Sum+=i"
- echo "1+2 、、、 + $num = $sum"
- + Else
- "Sum=sum+i"
- "I++"
- + fi
- Done
4. Example of a while loop with command-line control
- 1 #!/bin/sh
- 2
- 3 echo "Please input arguements is $#"
- 4 echo "What do you input:"
- 5 while [[$*! =] "]
- 6 do
- 7 echo $
- 8 shift
- 9 Done
While loop "Go" in the shell