Shell script (3)-loop statement

Source: Internet
Author: User
Tags case statement

For statement
1. For statement
1) Introduction
In the actual work, often encounter a task to be executed multiple times. Each execution is simply a different object, with the same other commands. For example, create a system account based on the list of names in your address book, check the surviving status of each host based on the list of services, and more.
When faced with various lists of repetitive tasks, the use of simple if statements has been difficult to meet the requirements, and the sequential writing of all the code is more cumbersome, difficult, and for loop statements can solve such problems.
2) For statement structure
For variable name in value list
Do
Command sequence
Done
Note: When you write a for statement, we first edit a value list file.
Vim Valueslist.txt

And then write the script
Vim aduser.sh

Execute the script and use tail-3 to view the first three rows of/etc/passwd and see the three users you just added

2. While statement
1) Introduction
The while statement is actually a looping statement, while the while statement is somewhat different from the for statement. The For statement applies to situations where the list object is irregular and the list source is fixed. The while statement is useful when you want to control the number of loops, the operands are numbered numerically, and repeat operations are performed on specific conditions.
2) While statement structure
While condition test action
Do
Command sequence
Done
Simple example: Batch build 20 users, and the user ID is a number identification.
Vim aduser2.sh

Verify

3. Case statement
1) Introduction
The case statement is primarily intended for situations where a variable has multiple values and needs to execute a different command sequence for each of these values, which is very similar to a multi-branch if statement, except that the statement needs to judge several different conditions and the case statement simply determines the different values of a variable
2) Case Statement structure
Case Variable value in
Mode 1)
Command sequence 1
;;
Mode 2)
Command Sequence 2
;;
。。。
*)
Default command sequence
Esac
Application Example: Edit a script that detects the input characters as follows: Vim chkchar.sh

Validation scripts

Shell script (3)-loop statement

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.