Fourth Chapter Shell Process Control

Source: Internet
Author: User
Tags case statement

Process Control is the instruction that alters the order of the program's operation.

4.1 If statement

4.1.1 Single Branch

an If condition expression; Then command fi

Example:

#!/bin/bashn=10if [$N-gt 5]; then echo yesfi# bash Test.shyes

4.1.2 Dual Branch

an If condition expression; Then command else command fi

Example:

#!/bin/bashn=10if [$N-lt 5]; Then echo yeselse echo nofi# bash Test.shno

4.1.3 Multi-Branch

an If condition expression; Then command elif conditional expression; Then command else command fi

Example:

#!/bin/bashn=10if [$N-gt 5]; Then echo "GT 5" Elif [$N-gt 6]; then echo "GT 6" Else Echo Nofi

If the first condition conforms, it no longer matches downwards.

4.2 For statement

The for variable name in the list of values; Do command done

Example:

#!/bin/bashfor i in {1..3}; Do echo $idone # bash test.sh123

The for syntax can also be written like this:

#!/bin/bashfor i in [email protected]; {echo $i}# bash test.sh 1 2 3123

The default for loop's list of values is delimited by a whitespace character, which is the $ifs in the system variable in the first chapter:

#!/bin/bashfor I in 12 34; Do echo $idone # bash test.sh1234

If you want to specify a delimiter, you can re-assign the $FS variable:

#!/bin/bashold_ifs= $IFSIFS = ":" str= "12:34:45" for I in $STR; do echo $idoneIFS = $OLD _ifs # restore Default # Bash a.sh123445

The For Loop also has a C-language-style syntax:

#!/bin/bashfor ((i=1;i<=5;i++)); Do Echo $idone


Blog Address: http://lizhenliang.blog.51cto.com

QQ Group: 323779636 (Shell/python devops Development Group)


4.3 While statement

while conditional expression; Do command done

Example 1:

#!/bin/bashn=0while [$N-lt 5]; Do let n++ echo $Ndone # bash test.sh12345

Terminates the loop when the conditional expression is true.

Example 2: A conditional expression of false will result in a dead loop

#!/bin/bashwhile [1-eq 1]; Do echo ' yes ' done

You can also use conditional expressions directly with true:

#!/bin/bashwhile true; Do echo ' yes ' done

You can also use the conditional expression with a colon, which in the shell means no action. But the status is 0, so true:

#!/bin/bashwhile:; Do echo ' yes ' done

Example 3: In a dead loop, the conditional termination loop is met

#!/bin/bashn=0while true; Do let n++ if [$N-eq 5]; Then break fi echo $Ndone # bash test.sh1234

It uses the If judgment and uses the break statement, which is jumping out of the loop. There is also a continue statement associated with it that jumps out of this loop.

Example 4: Examples illustrate continue usage

#!/bin/bashn=0while [$N-lt 5]; Do let n++ if [$N-eq 3]; Then continue fi echo $Ndone # bash test.sh1245

When the variable n equals 3 o'clock, continue skips the current loop and does not perform the following echo.

Note: Continue with the break statement can only be used in a loop statement.

Example 5: line-wise text

Text content:

# cat A.txta B C1 2 3x y Z

There are three ways to read the A.txt file line by row using the while loop:

Way 1:#!/bin/bashcat./a.txt | while read line; Do echo $LINEdone way 2:#!/bin/bashwhile read line; Do echo $LINEdone <./a.txt mode 3:#!/bin/bashexec <./a.txt # reads the file as standard output while the read line; Do Echo $LINEdone

There is also a until statement associated with the while, which differs from while in that it is only looping when the conditional expression is false and less in practice, which is no longer explained.

4.4 Case Statement

Case statements are typically used to selectively execute corresponding partial block commands.

Case Mode name in mode 1) command;;    Mode 2) command;; *) does not conform to the above mode execution command ESAC

Each pattern must end with a closing parenthesis and end with a double semicolon at the end of the command.

Example: Match different modes according to positional parameters

#!/bin/bashcase $ in start) echo "Start."    ;;        Stop) echo "stop."    ;;        Restart) echo "restart."    ;; *) echo "Usage: $ Start|stop|restart}" ESAC # bash test.shUsage:test.sh {start|stop|restart}# bash test.sh star tstart.# bash test.sh stopstop.# bash test.sh restartrestart.

The above example is not a bit familiar, under Linux a part of the service startup scripts are written in this way.

The pattern also supports regular, matching which pattern executes that:

#!/bin/bashcase $ in [0-9]) echo "match number."    ;;        [A-z])        echo "Match letter."    ;; '-H ' | '        --help ') echo "help" *) echo "Input error!" EXITESAC # bash test.sh 1match number.# bash test.sh amatch letter.# bash test.sh-hhelp# bash test.sh--helphelp

Patterns supported by: * 、?、 [], [.-.], |. The following sections explain the shell's regular expressions separately.

4.5 SELECT statement

Select is a statement that resembles a for loop.

Select variable in option 1 option 2; Do Breakdone

Example:

#!/bin/bashselect mysql_version in 5.1 5.6; Do echo $mysql _versiondone# bash Test.sh1) 5.12) 5.6#? 15.1#? 25.6

The user input number is assigned directly to the variable mysql_version. As a menu, the second time after the cycle will not show the menu, and can not meet the demand.

Add a dead loop outside and break one time with select once, so you can show the menu every time:

#!/bin/bashwhile true; Do select Mysql_version in 5.1 5.6; Do echo $mysql _version break donedone# bash Test.sh1) 5.12) 5.6#? 15.11) 5.12) 5.6#? 25.61) 5.12) 5.6

If you judge the number of user input to execute the corresponding command, if the multi-branch with the IF statement is much more complex, with case statement is much simpler.

#!/bin/bashps3= "select a number: " while true; do    select  Mysql_version in 5.1 5.6 quit; do        case   $mysql _version in            5.1)                  echo  "MySQL  5.1 "                 break                ;;             5.6)                  echo  "mysql 5.6"                  break                 ;;             quit)                  exit                 ;;             *)                   echo  "Input error, please  enter again! "                 break  &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;ESAC&NBSP;&NBSP;&NBSP;&NBSP;DONEDONE#&NBSP;BASH&NBSP;TEST.SH1)   5.12)  5.63)  quitselect a number: 1mysql 5.11) ( 5.12)  5.63)   quitselect a number: 2mysql 5.61)  5.12)  5.63)  quitselect a number :  3

If you do not want to use the default prompt, you can customize it by re-assigning the variable PS3. This is more perfect!


This article is from the "Li Zhenliang Technology Blog" blog, make sure to keep this source http://lizhenliang.blog.51cto.com/7876557/1882571

Fourth Chapter Shell Process Control

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.