The cyclic command is used to run a command or a group of commands for a specified number of times, or until a certain condition is met. Common loop statements in Bash shell include for loop, while loop, and until loop.
I. For Loop statements
1. For Loop syntax
For var in list
Do
Commands
Done
2. For Loop Flowchart
650) this. width = 650; "title =" image 067.png" src = "http://www.bkjia.com/uploads/allimg/131228/104QB0M-0.png"/>
3. For Loop example
1) enter a file to determine whether the file is directory or file.
1234567891011121314151617 |
[root@localhost test]# cat 3 .sh #!/bin/sh for file in $ 1 do
if [ -d "$file" ]
then
echo "$file is a directory"
elif [ -f "$file" ]
then
echo "$file is a file"
fi done [root@localhost test]# sh 3 .sh /etc/passwd /etc/passwd is a file [root@localhost test]# sh 3 .sh /etc /etc is a directory [root@localhost test]# |
Note:
Row 3: The location variable $1 is called.
Row 5-11: Use the if statement to determine whether $1 is a file or a directory.
2) calculate the number of files in a directory
123456789 |
[root@localhost test]# cat 4 .sh #!/bin/bash # Count= 0 for File in /tmp/*; do
file $File
Count=$[$Count+ 1 ] done echo "Total files: $Count." |
Note:
Http://t.163.com/event/info/eventId/-2934105915185819762
Http://t.163.com/event/info/eventId/7336578789135698864
Http://t.163.com/event/info/eventId/-2757900099931402204
Http://t.163.com/event/info/eventId/-6022217269862157430
Http://t.163.com/event/info/eventId/-6327288559455055957
Http://t.163.com/event/info/eventId/-5434619824903743434
Http://t.163.com/event/info/eventId/-7296668920759701419
Http://t.163.com/event/info/eventId/-6761754697175518719
Http://t.163.com/event/info/eventId/2714712527073182328
Http://t.163.com/event/info/eventId/-5002856799661246013
Http://t.163.com/event/info/eventId/-4996357046007032207
Http://t.163.com/event/info/eventId/1105816084476859073
Http://t.163.com/event/info/eventId/-8600093788630276814
Http://t.163.com/event/info/eventId/8431790839426740003
Http://t.163.com/event/info/eventId/-7330955025813744932
Http://t.163.com/event/info/eventId/8644906182003528012
Http://t.163.com/event/info/eventId/7423718734365387206
Http://t.163.com/event/info/eventId/8142539201743089519
Http://t.163.com/event/info/eventId/6894078421345426422
Http://t.163.com/event/info/eventId/-6211762240931380686
Http://t.163.com/event/info/eventId/-8088822526390658491
Http://t.163.com/event/info/eventId/5348067141736717459
Http://t.163.com/event/info/eventId/1446077859086446713
Http://t.163.com/event/info/eventId/-3875504737289263757
Http://t.163.com/event/info/eventId/-7305448483219754978
Http://t.163.com/event/info/eventId/-6331199466714658720
Http://t.163.com/event/info/eventId/-4025232118219909260
Row 7: The Count value of each loop + 1
Ii. While loop statements
The while command allows you to define the command to be tested. If the defined test command returns the 0 state value, execute the statement in the while loop. Otherwise, exit directly.
1) while loop syntax
While test command
Do
Oter command
Done
2) while loop Flowchart
650) this. width = 650; "title =" image 068.png" src = "http://www.bkjia.com/uploads/allimg/131228/104Q61G3-1.png"/>
3) while loop example
Calculate the sum of integers less than 100
123456789 |
[root@localhost test]# cat 6 .sh #!/bin/sh Sum= 0 Count= 1 while [ $Count -le 100 ]; do
let Sum+=$Count
let Count++ done echo $Sum |
If the user's ID number is an even number, the name and shell are displayed. This operation is performed on all users;
12345678 |
[root@localhost test]# cat 5 .sh #!/bin/sh while read LINE; do
Uid=`echo $LINE | cut -d: -f3`
if [ $[$Uid% 2 ] -eq 0 ]; then
echo $LINE | cut -d: -f1, 7
fi done < /etc/passwd |
Note: