Loop statements commonly used in scripts

Source: Internet
Author: User

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 forfile in$1doif[ -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 isa file [root@localhost test]# sh 3.sh /etc /etc isa 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=0forFile in/tmp/*; dofile $File Count=$[$Count+1done 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=0Count=1while[ $Count -le 100]; dolet 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 whileread LINE; doUid=`echo $LINE | cut -d: -f3` if[ $[$Uid%2] -eq 0]; then echo $LINE | cut -d: -f1,7fi done < /etc/passwd

Note:

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.