1 For Loops
Structure:
For ARG in [LIST]
Do
Command (s )...
Done
#! /Bin/bashsum = 0for int in 1 2 3 4 5do sum = 'expr $ sum + $ int 'doneecho $ sum
2 while
Determine whether the condition is met at the beginning of the loop.,If the conditions are always met,Then keep repeating. AndForThe difference between loops is:,This structure is applicable when the number of loops is unknown.
Structure:
While [condition]
Do
Command...
Done
Output sum of squares of numbers less than 100
#! /Bin/bashnum = 1squ = 0 while [$ num-Le 100] Do squ = 'expr $ num \ * $ num' echo $ squ num = 'expr $ num + 1' done echo "Up is num's SQU, num< 100"
Note:
When assigning values to variables, do not place spaces before and after the equal sign.
-A space is blank before and after le.
When expr is used, the symbol before and after the entire sentence is not a single quotation mark. It is the one above a tab key ~ ....
In the middle of num *, there is an anti-oblique carry \
3 until
This structure determines the condition at the top of the loop,And if the condition is alwaysFalseThen keep repeating.(AndWhileOpposite)
Structure:
Until [condition-is-True]
Do
Command ..
Done
Usage andWhileSimilar, not for example
#! /Bin/bashnum = 1squ = 0 until [$ num-GT 10] Do squ = 'expr $ num \ * $ num' echo $ squ num = 'expr $ num + 1' done echo "Up is num's SQU, num< 100"