Process Control of shell scripts

Source: Internet
Author: User
Tags glob

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
        • Create an infinite loop
      • Special usage of While
      • Special usage of For
      • Select loops and Menus
        • Select and case
      • 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:

    1. *:任意长度任意字符
    2. ?:任意单个字符
    3. []:指定范围内的任意单个字符
    4. 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

    1. #!/bin/bash
    2. # Name: doit.sh
    3. # Purpose: shift through command line arguments
    4. # Usage: doit.sh [args]
    5. while[ $# -gt 0 ] # or (( $# > 0 ))
    6. do
    7. echo $*
    8. shift
    9. done
    1. #!/bin/bash
    2. #step through all the positional parameters
    3. until[-z "$1"]
    4. do
    5. echo "$1"
    6. shift
    7. done
    8. echo
Create an infinite loop
    1. while true;do
    2. 循环体
    3. done
    1. until false;do
    2. 循环体
    3. 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:

    1. for((i=0;i<=20;i++))
    2. do
    3. echo "Helllo I am $i"
    4. 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:

  1. PS3="please input numbert: "
  2. select menu in羊肉汤米饭胡辣汤饺子拉面烩面 quit;do
  3. case $REPLY in
  4. 1)
  5. echo "羊肉汤,the price is 15."
  6. ;;
  7. 2)
  8. echo "米饭,the price is 12."
  9. ;;
  10. 3)
  11. echo "胡辣汤,the price is 30."
  12. ;;
  13. 4)
  14. echo "饺子,the price is 20."
  15. ;;
  16. 5)
  17. echo "拉面,the price is 12."
  18. ;;
  19. 6)
  20. echo "烩面,the price is 10."
  21. ;;
  22. 7)
  23. break
  24. ;;
  25. esac
  26. done
Signal Capture Trap
trap ‘触发指令‘ 信号    自定义进程收到系统发出的指定信号后,将执行触发指令,而不会执行原操作trap ‘‘ 信号    忽略信号的操作trap ‘-‘ 信号    恢复原信号的操作trap -p    列出自定义信号操作

Example:

  1. #!/bin/bash
  2. trap ‘echo "signal:SIGINT"‘ int
  3. trap -p
  4. for((i=0;i<=10;i++))
  5. do
  6. sleep 1
  7. echo $i
  8. done
  9. trap ‘‘ int
  10. trap -p
  11. for((i=11;i<=20;i++))
  12. do
  13. sleep 1
  14. echo $i
  15. done
  16. trap ‘-‘ int
  17. trap -p
  18. for((i=21;i<=30;i++))
  19. do
  20. sleep 1
  21. echo $i
  22. done

Process Control of shell scripts

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.