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