Shell script is our learning operational difficulties, this content simple example shell script advanced, there are any technical guidance, knowledge of the shortcomings are welcome to point out.
Process Control
Programming languages: Sequential execution selection execution loop execution
Conditional Select if statement
? Single Branch
if judgment condition; then condition is true Branch code fi
Double-branch if judging condition; Then condition for true Branch code else condition for false Branch code FI
Multi-Branch if judging condition 1; Then condition is true branch code elif judging condition 2; Then condition is true branch code elif judging Condition 3; Then condition is true branch code else above condition is false Branch code FI
Judge by condition, when the first encounter is a "true" condition, execute its branch, and then end the entire if statement
Example:
Write script/root/bin/filetype.sh, determine user input file path, display its file type (normal, directory, link, other file type)
Execution results are as follows
Example 2
Write script/root/bin/checkint.sh to determine if the user input parameter is a positive integer
Execution results are as follows
Conditional Judgment: Case statement
Case variable reference in
PAT1) branch 1;;
PAT2) branch 2;;
.. *) default branch;;
Esaccase supports GLOB-style wildcard characters: *: Any character of any length?: any single character []: any single character in the specified range a|b:a or b
Example
Create a pin name of useradd.sh, when the-a option is executed, the user Mageuser1~mageuser20 is created in bulk, requiring the Mageuserx uid to be 200x,mageuserxx uid for the 20xx,1~8 user's shell to/ The UID of the sbin/nologin,9~20 user is/bin/bash. The last 20 user passwords are set to CentOS when the-D option is executed, the user MAGEUSER1~MAGEUSER20 is deleted in bulk. Delete together with home directory
Cycle
Loop execution repeats how many times a piece of code is run repeatedly: number of cycles known beforehand that there is an entry condition and exit condition
For loop
For variable name in list;
Loop body
Done
Example
Example
Writing a script print rectangle
The results of the implementation are as follows:
While loop
? While CONDITION; Do
Loop body
Done
? CONDITION: cyclic control conditions; Before entering the cycle, make a judgment; once each loop is judged again; the condition is "true", then a loop is executed until the condition test state is "false" to terminate the loop
? Therefore: Condtion generally should have a cyclic control variable, and the value of this variable will be continuously corrected in the loop body
? Enter condition: Condition is true
? Exit Condition: Condition is false
Example
Loop Control Shift Command
? Shift [N]
? Used to move the list of parameters to the left for a specified number of times, the default is to move left once.
? Parameter list once moved, the left-most argument is removed from the list. While looping through positional parameter lists, use the shift
?./doit.sh a b c d e F g h
?./shfit.sh a b c d e F g h
Example
Execution results are as follows
Create an infinite loop
? While true; Do
Loop body
? Done
? until false; Do
Loop body
? Done
(shell script next week after the advanced updated)
Shell script Advanced (top)