Loop execution: Repeats a piece of code 0, 1, or more times;
Enter the condition: when the condition satisfies, only then enters the circulation;
Exit conditions: Each cycle should have an exit condition to have the opportunity to exit the loop;
Bash script:
For loop
While loop
Until cycle
For loop:
Two types of formats:
(1) Traversing the list
(2) Control variables
(1) Traversing the list:
For varaible in LIST; Do
Loop body
Done
Entry conditions: As long as the list has elements, you can enter the loop;
Exit Criteria: The element traversal in the list is completed;
How the LISTT is generated:
(1) give directly;
(2) List of integers
(a) {start: End
(b) SEQ [start [incremtal]] Last
(3) command to return a list
(4) Glob
(5) Variable reference
[email protected], $*
Example: adding three users
#!/bin/bash#for username in User21 user22 user23; Do if ID $username &>/dev/null; Then echo "$username exists." else useradd $username && echo "Add user $username finished." Fidone
Example: The sum of all positive integers within 100 is obtained;
#!/bin/bash#declare-i sum=0for i in {1..100}; Do echo "\ $sum are $sum, \ $i is $i" sum=$[$sum + $i]doneecho $sum
Example: Determine the content type of each file under the/var/log directory
#!/bin/bash#for filename in /var/log/*; do if [ -f $ filename ]; then echo "Common file." elif [ -d $filename ]; then echo "Directory." elif [ -L $filename ]; then echo "Symbolic link." elif [ -b $filename ]; then echo "Block special file." elif [ -c $filename ]; then echo "Character special file." elif [ -S $filename ]; then echo "Socket file." else echo " unkown. " fidone
For loop format:
For varaible in LIST; Do
Loop body
Done
For loop implementation 9*9 multiplication table
#!/bin/bash#for j in {1..9}; Do for I in $ (seq 1 $j); Do Echo-n-E "${i}x${j}=$[${i}*${j}]\t" done echo-Done
While loop:
While CONDITION; Do
Loop body
Cyclic control variable correction expression
Done
Entry condition: Condition test is true
Exit Condition: Condition test is "false"
Example: The sum of all positive integers within 100 is obtained;
#!/bin/bash#declare-i sum=0declare-i i=1while [$i-le 100]; Do let sum+= $i let I++doneecho $sum
Until cycle:
Until CONDITION; Do
Loop body
Cyclic control variable correction expression
Done
Entry condition: Condition test for "false"
Exit Condition: Condition test is true
Example: The sum of all positive integers within 100 is obtained;
#!/bin/bash#declare-i sum=0declare-i i=1until [$i-gt 100]; Do let sum+= $i let I++doneecho $sum
Special usage of the while loop (the line that traverses the file):
while read VARIABLE; Do
Circulation body;
Done </path/from/somefile
Read each line in the/path/from/somefile file sequentially and assign the base value to the variable variable;
Example: Find the user whose ID number is even, display its user name, ID and default shell;
#!/bin/bash#while read line; Do userid=$ (echo $line | cut-d:-F3) username=$ (echo $line | cut-d:-F1) usershell=$ (echo $line | cut-d:-F7) If [$[$userid%2]-eq 0]; Then echo "$username, $userid, $usershell." Fidone </etc/passwd
Special usage for the FOR loop:
For (control variable initialization; conditional judgment expression; control variable correction statement)); Do
Loop body
Done
Control variable initialization: Executes only once when the loop code begins to run;
The correction of the control variables: each round of the end of the cycle will be the control variable correction operation, and then make the conditional judgment;
Example: Sum of all positive integers within 100
#!/bin/bash#declare-i sum=0for ((i=1;i<=100;i++)); Do let sum+= $idoneecho "sum: $sum."
Example: printing a 99 multiplication table
#!/bin/bash#for ((j=1;j<=9;j++)); Do for ((i=1;i<=j;i++)); Do echo-e-n "${i}x${j}=$[${i}*${j}]\t" done Echodone
This article is from the "Wang Liming" blog, make sure to keep this source http://afterdawn.blog.51cto.com/7503144/1916005
For/while/untill Loop of shell script programming loops