Chapter 6 of linuxshell programming guide ------ control flow structure 1

Source: Internet
Author: User
As mentioned above, expr indicates that this command is used to add a count in a loop. The following uses ls to list the objects and their numbers in the for loop. [Root @ localhosthuangcd] catforcount! Binbashcounter = 0forfilesin * do

In the previous discussion on e x p r, we pointed out that this command is used if the count is to be added during the loop. The following uses l s to list in the f o r loop
File and its number.

[Root @ localhost huangcd] # cat forcount

#! /Bin/bash
Counter = 0
For files in *
Do
Counter = 'expr $ counter + 1'
Done
Echo "there are $ counter files in 'pwd' we need to process"
[Root @ localhost huangcd] # sh forcount
There are 76 files in/home/huangcd we need to process
[Root @ localhost huangcd] # ls | wc-l

The nested loop can embed one f o r loop into another f o r loop:
For variable name 1 in list 1
D o
For variable name 2 in list 2
D o
Command 1
...
D o n e
D o n e

The following script is embedded into the f o r loop. here there are two lists: a p s and s c r I P T S. The first contains the application on the server
The second is the management script running on each application. For each application on the list a p s, the list
The script in s c r I P T S will be run, and the script is actually run in the background. The script uses the t e command to put
So that the output to the screen is also output to a file. View the output result to see how to embed the f o r loop
Use the list s c r I P T S to execute the processing on the list A P S.

[Root @ localhost huangcd] # cat audit_run
#! /Bin/bash
APPS = "/apps/accts/apps/claims/apps/stock/apps/serv"
SCRIPTS = "audit. check report. run cleanup"
LOGFILE = audit. log
MY_DATE = 'date + % H: % M "on" % d/% m % Y'


For loop in $ APPS
Do
For loop2 in $ SCRIPTS
Do
Echo "system $ loop now running $ loop2 at $ MY_DATE" | tee-a $ LOGFILE $ loop $ loop2
Done
Done

U n t I l executes a series of commands cyclically until the condition is true. The processing method of the u n t I l loop and the w h I l e loop is exactly the same
On the contrary. Generally, the w h I l e loop is better than the u n t I l loop, but in some cases, the u n t I l loop is more
Useful.
The format of the u n t I l loop is:
Until condition
Command 1
...
D o n e
The condition can be any test condition, and the test occurs at the end of the loop, so the loop is executed at least once-please note this.

This script constantly searches for the user r o t in the w h o command, and the variable I S-R O T saves the results of the g r e p command.
If the r o t is found, the loop ends, and an email is sent to the user s I o n to notify him that the user r o t has logged on. Note that
Here s l e p command usage, which is often used in the u n t I l loop, because the body must sleep for several seconds before execution,
Otherwise, a large amount of system resources will be consumed.

[Root @ localhost huangcd] # cat until_who
#! /Bin/bash
IS_ROOT = 'Who | grep root'
Until ["$ IS_ROOT"]
Do
Sleep 5
Done
Echo "watch it. roots in" | mail root
[Root @ localhost huangcd] # sh until_who
[Root @ localhost huangcd] # mail
Mail version 8.1 6/6/93. Type? For help.
"/Var/spool/mail/root": 3 messages 1 new 3 unread
U 1 logwatch@localhost.l Sat Oct 26 113/3545 "Logwat"
U 2 logwatch@localhost.l Wed Dec 11 90/3152 "Logwat"
> N 3 root@localhost.local Wed Dec 11 15/646
& 3
Message 3:
From root@localhost.localdomain Wed Dec 11 16:09:29 2013
Date: Wed, 11 Dec 2013 16:09:29 + 0800
From: root
To: root@localhost.localdomain

Watch it. roots in

The w h I l e loop can be used to read keyboard information. In the following example, the input information is set to the variable f I L M, and press <C t r l-
D> end the loop.

[Root @ localhost huangcd] # cat whileread
#! /Bin/bash
Echo "type ctrl + d to terminate"
Echo-n "enter your most like file :"
While read FILE
Do
Echo "yeah, great file the $ FILE"
Done
[Root @ localhost huangcd] # sh whileread
Type ctrl + d to terminate
Enter your most like file: ddfdfdf
Yeah, great file the ddfdfdf
Dfsf
Yeah, great file the dfsf

The w h I l e loop is most commonly used to read data from a file, so writing a script can process such information.
Assume that you want to read information from a file containing the employee name, subordinate department, and I D.

You can use a variable to save each row of data. when no data is read, the condition is true. W h I l e loop using input reset
To ensure that data is read from the file. Note that the entire row of data is set as a single variable $ l I N E ..

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.