Shell script programming includes process control, functions, arrays, advanced string manipulation, and advanced variables, where the main introduction to Process control.
Process Control is mainly a process programming language, which is divided into sequential execution, selection execution and cyclic execution.
Conditional Select if statement
If statements are divided into a single branch, two branches and multi-branch three, a single branch only need to enter a period of true branch code can be, and the double branch needs to enter two segments of code, a condition is true, a condition is false, multi-branch more complex, one by one, the first encounter the true condition, the execution of the branch, and then
Single Branch: if Judging conditions;then The conditions are true for the branch code fi Dual Branch: if Judging conditions;then The conditions are true for the branch code else conditions for false branch codes fi &nbSp; fi
Multi-branch: If judge condition 1;then condition is true branch code elif Judge condition 2;then condition is true branch code elif judging condition 3;t The hen condition is true for the branch code else above conditions are false for the branch code FI
Example:
Edit Script/root/bin/fi1.sh, to achieve the following functions: Enter a number, when this number equals 5 o'clock, output middle, when this number is greater than or equal to 0 less than 5 o'clock, output small, when this number is less than 0 o'clock, output error, otherwise, output big.
#!/bin/bashif [$1-eq 5];then echo "Middle" elif [$1-lt 5-a $1-ge 0];then echo "small" elif [$1-lt 0 ];then echo "error" Else echo "big" fi
Note: Use the "" to be aware of the conditions before you have a space before the completion of the script, do not forget to add execute permissions, with. or source to execute the script.
2. Conditional Judgment Case Statement
The case statement refers to a variable that is then entered into each branch, ending with Esac.
Case variable reference inPAT1) branch 1;; PAT2) branch 2;; .. *) default branch;; Esac
Note: Case 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:
Edit Script/root/bin/case1.sh, if the input is Boy,m,ae,n,male, then output gentleman, if the input is Girl,wom,ae,n,female, then output lady, otherwise, output error.
#!/bin/bashcase $ in Boy|m[ae]n|male) echo "gentleman";; Girl|wom[ae]n|female) echo "Lady";; *) echo "Error";; Esac
Note: do not forget ";;" after each line of code has been written.
Cyclic execution is a more important part. Loop execution is to run a code segment repeatedly, with entry conditions and exit criteria.
3.for Cycle
Assigns the element in the list to the "variable name" in turn; The loop body is executed once each assignment; Until the elements in the list are exhausted, the loop ends
For variable name in list; Do loop body done
List Generation Method:
(1) Give the list directly
(2) List of integers:
(a) {start: End
(b) $ (SEQ [start [step]] end)
(3) command to return a list
$ (COMMAND)
(4) using glob, such as: *.sh
(5) variable reference;
[email protected], $*
Example:
Edit Script/root/bin/xin.sh, implement print hearts Rectangle
#!/bin/bashfor i in {1..10};d o-N in {1..10};d o echo-en ' \033[31m?\033[0m ' do echo done
Note: For loop can set other loops, other loops can also set for loop
4.while Cycle
While loop control condition; Do loop body done
Experiment: Show partitions with a partition utilization greater than 10% Sda5 'll be full:30%
#!/bin/bashdf |grep SD |while read disk;d o diskname=$ (echo $disk |cut-d "-f1) diskusage=$ (echo $disk |sed-r ' s/ . * ([0-9]+)%.*/\1/') [$diskusage-ge] && echo "$diskname'll be full: ${diskusage}%" done
CONDITION: cyclic control conditions; Before entering the cycle, make a judgment; each cycle will be judged again; conditions
Is true, the loop is executed once, until the condition test state is "false" to terminate the loop
Condtion generally should have cyclic control variables, and the value of this variable will be continuously corrected in the loop body
Enter condition: Condition is true
Exit Condition: Condition is false
While CONDITION; Do Loop body done
5. Circular Control Statement continue
Used in the loop body
Continue [n]: End of the nth layer of the current cycle, and directly into the next round of judgment; the inner layer is the 1th floor.
While CONDTIITON1; Do CMD1 ... if CONDITION2; Then continue fi cmdn ... done
Example:
Using the while loop output 1~9
While [$n-lt];d O-echo $n let n++ if [$n-eq 5];then Continue Fidone
6. Loop Control Statement Break
Used in the loop body
Break [n]: Early end of the nth layer cycle, the inner layer is the 1th layer
While CONDTIITON1; Do CMD1 ... if CONDITION2; Then break fi cmdn ... done
7.until Cycle
Entry condition: CONDITION is False
Exit Condition: CONDITION is True
Until CONDITION; Do Loop body done
Example:
Using until loop output 1~09
Until [$n-eq];d o echo $n let N++done
Note: The Until loop statement condition is false in order to cycle down, with the while loop in addition to the same judging conditions.
8. Loop CONTROL Statement Shift
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 loop
When traversing a positional parameter list, use the shift
./doit.sh a b c d e F g h
Example:
doit.sh
#!/bin/bash# name:doit.sh# purpose:shift through command line arguments# Usage:doit.sh [args]while [$#-GT 0] # or (( $# > 0) do echo $* Shiftdone
Shell Programming Advanced