Chapter 2. Learning shell scripts Loop)

Source: Internet
Author: User

 

While do done, until do done (indefinite loop)

 

While [condition] <= the state in the brackets is the begin DO <= do is the beginning of the loop! Program section done <= done is the end of the loop

 

Until [condition] Do program section done

 

[[email protected] scripts]# vi sh13.sh#!/bin/bash# Program:#Repeat question until user input correct answer.# History:# 2005/08/29VBirdFirst releasePATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport PATHwhile [ "$yn" != "yes" -a "$yn" != "YES" ]doread -p "Please input yes/YES to stop this program: " yndoneecho "OK! you input the correct answer."

 

[[email protected] scripts]# vi sh13-2.sh#!/bin/bash# Program:#Repeat question until user input correct answer.# History:# 2005/08/29VBirdFirst releasePATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport PATHuntil [ "$yn" == "yes" -o "$yn" == "YES" ]doread -p "Please input yes/YES to stop this program: " yndoneecho "OK! you input the correct answer."

 

[[Email protected] Scripts] # vi sh14.sh #! /Bin/bash # program: # Use loop to calculate "1 + 2 + 3 +... + 100 "result. # History: #2005/08/29 vbirdfirst releasepath =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ /Binexport paths = 0 # This is the total value variable I = 0 # This is the cumulative value, that is, 1, 2, 3... while ["$ I "! = "100"] Doi = $ ($ I + 1) #1 s is added every time I is added = $ ($ S + $ I )) # Add once every time! Doneecho "the result of '1 + 2 + 3 +... + 100 'is => $ S"
For... do... done (fixed loop)

The for syntax is in the state of "you already know how many times you want to repeat! His syntax is:

For VaR in con1 con2 con3... Do program segment done

In the preceding example, when the variable content of $ VaR is working in the Loop:

  1. For the first round, the content of $ VaR is con1;
  2. For the second round, the content of $ VaR is con2;
  3. For the third round, the content of $ VaR is con3;
  4. ....

We can make a simple exercise. Suppose I have three animals: dog, cat, and elephant. If I want to output the following words in each row: "There are dogs...", you can:

[[email protected] scripts]# vi sh15.sh#!/bin/bash# Program:# Using for .... loop to print 3 animals# History:# 2005/08/29VBirdFirst releasePATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport PATHfor animal in dog cat elephantdoecho "There are ${animal}s.... "done

 

[[Email protected] Scripts] # vi sh16.sh #! /Bin/bash # Program # Use Id, finger command to check system account's information. # history #2009/02/18 vbird first releasepath =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ /Binexport pathusers = $ (cut-d': '-F1/etc/passwd) # retrieve the account name for username in $ users # Start recycling! Do ID $ username finger $ usernamedone

 

[[Email protected] Scripts] # vi sh17.sh #! /Bin/bash # Program # Use ping command to check the network's PC state. # history #2009/02/18 vbird first releasepath =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ /Binexport pathnetwork = "192.168.1" # first define the first part of a domain! For sitenu in $ (SEQ 1 100) # seq stands for sequence (continuous). Do # whether the program under the system gets the ping return value correctly or fails! Ping-C 1-W 1 $ {network }. $ {sitenu} &>/dev/null & Result = 0 | result = 1 # start to show whether the result is correctly started (up) or wrong (down) if ["$ result" = 0]; then Echo "server $ {network }. $ {sitenu} is up. "Else echo" server $ {network }. $ {sitenu} is down. "fidone

After the above commands are run, 192.168.1.1 ~ is displayed ~ 192.168.1.100 a total of 100 hosts can be connected to your machine now! If your domain is different from that of laruence, you can directly modify the variable content of the network on the top! In fact, the focus of this example is $ (SEQ! That seq is the abbreviation of sequence! It indicates that the two values following are continuous!

 

[[Email protected] Scripts] # vi sh18.sh #! /Bin/bash # program: # user input dir name, I find the permission of files. # History: #2005/08/29 vbirdfirst releasepath =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ /Binexport path #1. check whether this directory exists first? Read-P "Please input a directory:" dirif ["$ dir" = ""-o! -D "$ dir"]; thenecho "the $ DIR is not exist in your system." Exit 1fi #2. Start the test file ~ Filelist = $ (ls $ DIR) # list all file names in this directory for filename in $ filelistdoperm = "" test-R "$ DIR/$ FILENAME" & perm = "$ perm readable" test-W "$ DIR/$ FILENAME" & perm = "$ perm writable" test-X "$ DIR/$ FILENAME" & perm = "$ perm executable" Echo "the file $ DIR/$ filename's permission is $ perm "done

 

For... do... done numerical processing

 

For (initial value; Restriction value; running step) Do program segment done

 

  • Initial Value: the initial value of a variable in the loop, which is directly configured with something similar to I = 1;
  • Limit Value: when the value of a variable is within the limit value, the loop continues. For example, I <= 100;
  • Running steps: the variable changes every time a loop is made. For example, I = I + 1.

It is worth noting that in the configuration of "Running step", if you add 1 at a time, you can use a method similar to "I ++, that is to say, I adds one for every loop.

[[email protected] scripts]# vi sh19.sh#!/bin/bash# Program:# Try do calculate 1+2+....+${your_input}# History:# 2005/08/29VBirdFirst releasePATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport PATHread -p "Please input a number, I will count for 1+2+...+your_input: " nus=0for (( i=1; i<=$nu; i=i+1 ))dos=$(($s+$i))doneecho "The result of ‘1+2+3+...+$nu‘ is ==> $s"

 

 

Chapter 2. Learning shell scripts Loop)

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.