Process Control of shell scripts
- Process Control of shell scripts
- Conditional statements
- Conditional judgment
- Loop Statement For,while,until
- For loop
- While loop
- Until cycle
- Loop Control statement Continue
- Loop control Statement Break
- Loop Control Shift Command
- Special usage of While
- Special usage of For
- Select loops and Menus
- Signal Capture Trap
conditional Statements
Select Execute:
Note: If statements can be nested
Single Branch
if 判断条件;then 条件为真的分支代码fi
Dual Branch
if 判断条件; then 条件为真的分支代码else 条件为假的分支代码fi
Multi-Branch
if 判断条件1; then 条件为真的分支代码elif 判断条件2; then 条件为真的分支代码elif 判断条件3; then 条件为真的分支代码else 以上条件都为假的分支代码fi
Judge by condition, when the first encounter is a "true" condition, execute its branch, and then end the entire if statement
Example:
if Ping-C1-W2Station1 &>/dev/NULL; Then Echo'Station1isup' elif grep"Station1"~/maintenance.txt&>/dev/NULL; Then Echo'station1isundergoingmaintenance 'Else Echo'station1isunexpectedlydown!'exit1fi
Conditional Judgment
case 变量引用inPAT1) 分支1 ;;PAT2) 分支2 ;;...*) 默认分支 ;;esac
Case supports GLOB-style wildcard characters:
*:任意长度任意字符
?:任意单个字符
[]:指定范围内的任意单个字符
a|b: a或b
Loop Statement For,while,until
For loop
for 变量名in 列表;do 循环体done
Implementation mechanism:
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
列表生成方式
:
- (1) Give the list directly
- (2) List of integers:
- (a) {start: End
- (b) $(Seq[start [step]] end)
- (3) command to return a list
- (4) using glob, such as: *.sh
- (5) variable reference;$@, $*
While loop
while CONDITION; do 循环体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
进入条件
: Condition is True
退出条件
: Condition is False
Until cycle
until CONDITION; do 循环体done
Condition with while
进入条件
: CONDITION is False
退出条件
: CONDITION is True
loop Control statement continue
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 Do CMD1 ... if Then continueficmdn ... Done
loop control Statement
break
Break [n]: Early end of the nth layer cycle, the inner layer is the 1th layer
while CONDTIITON1; doCMD1...if CONDITION2; then breakfiCMDn...done
loop Control Shift command
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. The while loop iterates through the position parameter list, often to shift.
./doit.sh a b c d e f g h
./shfit.sh a b c d e f g h
#!/bin/bash
# Name: doit.sh
# Purpose: shift through command line arguments
# Usage: doit.sh [args]
while[ $# -gt 0 ] # or (( $# > 0 ))
do
echo $*
shift
done
#!/bin/bash
#step through all the positional parameters
until[-z "$1"]
do
echo "$1"
shift
done
echo
Create an infinite loop
while true;do
循环体
done
until false;do
循环体
done
special usage of
while
Special usage of the while loop (traversing each line of the file):
while read line; do 循环体done < /PATH/FROM/SOMEFILE
Read each line in the/path/from/somefile file sequentially and assign the row to the variable line
special usage of for
Double-brace method, i.e. ((...)) Format, which can also be used for arithmetic operations
The double-brace method also enables the bash shell to implement C-style variable manipulation
i=10
((i++))
for ((控制变量初始化;条件判断表达式;控制变量的修正表达式))do 循环体done
Control variable initialization: Executes only once when running to a loop code snippet
Correction expressions for control variables: The control variable correction operation is performed at the end of each cycle, then the condition is judged.
For example:
for((i=0;i<=20;i++))
do
echo "Helllo I am $i"
done
Select loops and Menus
select variable in listdo 循环体命令done
The Select loop is used primarily to create menus , and menu items in numerical order are displayed on standard errors and displayed PS3 提示符
, waiting for user input.
The user enters a number in the menu list and executes the corresponding command
User input is saved in built-in variables REPLY
Select and case
Select is an infinite loop, so remember to exit the loop with the break command, or terminate the script with the Exit command. You can also press CTRL + C to exit the loop.
Select is often used in conjunction with case
Similar to a For loop, you can omit the in list and use positional parameters at this time
Example:
PS3="please input numbert: "
select menu in羊肉汤米饭胡辣汤饺子拉面烩面 quit;do
case $REPLY in
1)
echo "羊肉汤,the price is 15."
;;
2)
echo "米饭,the price is 12."
;;
3)
echo "胡辣汤,the price is 30."
;;
4)
echo "饺子,the price is 20."
;;
5)
echo "拉面,the price is 12."
;;
6)
echo "烩面,the price is 10."
;;
7)
break
;;
esac
done
Signal Capture Trap
trap ‘触发指令‘ 信号 自定义进程收到系统发出的指定信号后,将执行触发指令,而不会执行原操作trap ‘‘ 信号 忽略信号的操作trap ‘-‘ 信号 恢复原信号的操作trap -p 列出自定义信号操作
Example:
#!/bin/bash
trap ‘echo "signal:SIGINT"‘ int
trap -p
for((i=0;i<=10;i++))
do
sleep 1
echo $i
done
trap ‘‘ int
trap -p
for((i=11;i<=20;i++))
do
sleep 1
echo $i
done
trap ‘-‘ int
trap -p
for((i=21;i<=30;i++))
do
sleep 1
echo $i
done
Process Control of shell scripts