Shell Process Control

Source: Internet
Author: User
Tags case statement closing tag

Unlike Java, PHP and other languages, SH's process control is not empty, such as (the following is the PHP Process Control notation):

<? PHP if (Isset ($_get["q"]) {    search (q);} Else {    //  don't do anything }

You can't write this in Sh/bash, and if the Else branch doesn't have a statement execution, don't write this else.

If ElseIf

If statement syntax format:

if conditionthen    command1    command2    ... CommandN fi

Write a line (for the terminal command prompt):

if " SSH " 1 " true "; Fi

At the end of the fi is if the upside-down spelling, and later will encounter similar.

If else-if Else

If else-if else syntax format:

if condition1then    command1elif condition2 then     command2else    commandnfi 

The following example determines whether two variables are equal:

#!/bin/sha=Tenb=6if[$a = =$b]then Echo"a equals b"elif [$a-GT $b]then Echo"a greater than B"elif [$a-LT $b]then Echo"A is less than B"ElseEcho"A is not equal to B"fi

Execution Result:

A greater than B

The If Else statement is often used in conjunction with the Test command, as shown below

num1=$[2*3]num2=$[1+5]if test $[num1]-eq $[ Num2]then    ' two numbers equal! ' Else ' Two numbers are not equal!       ' fi

Output Result:

A equals b
For loop

Similar to other programming languages, the shell supports A for loop.

The general format for A for loop is:

 for var inch item1 item2 ... itemn  Do     command1    command2    ... Commandndone

Write a line:

 for var inch  do Command1; Command2. Done;

When the value of the variable is in the list, the For loop executes all commands once, using the variable name to get the current value in the list. The command can be any valid shell command and statement. The in list can contain replacements, strings, and file names.

The in list is optional, if it is not used, the For loop uses the command-line positional parameters.

For example, sequentially output the numbers in the current list:

#!/bin/shforvarin123456    Do "The order of the numbers is $var" Done

Output Result:

The order of the numbers for the order of 1 numbers for the order of 2 numbers for the order of 3 numbers for the order of the 4 numbers for 5 numbers is 6
While statement

The while loop is used to continuously execute a series of commands and to read data from the input file; the command is usually a test condition. The format is:

 while condition  Do     Commanddone

The following is a basic while loop, and the test condition is: If int is less than or equal to 5, then the condition returns True. int starts at 0, and each time the loop is processed, int plus 1. Run the above script, return the number 1 to 5, and then terminate.

#!/bin/Shi=0while (($i <=5))does" $i "  "i++"

Run script, Output:

0 1 2 3 4 5

Using the Bash let command, which executes one or more expressions, the variable calculation does not need to add $ to represent the variable.

The Let command is a tool for calculations in BASH that executes one or more expressions and does not need to be added as a variable in the calculation of variables. If a space or other special character is included in an expression, it must be caused.

Infinite loops

Infinite loop Syntax format:

 while  :  Do     Commanddone

Or

 while true  Do     Commanddone

Or

 for (( ; ; ))
Until cycle

The Until loop executes a series of commands until the condition is true.

The Until loop and the while loop are just the opposite of the processing mode.

The general while loop is better than the until loop, but at some point-and only in rare cases-the until loop is more useful.

Until syntax format:

until condition  Do     Commanddone

condition is generally a conditional expression, and if the return value is false, the statement in the loop body continues to execute, otherwise it jumps out of the loop.

The following example uses the until command to output a 0 to 9 number:

#!/bin/Basha=0  ]do   echo $a   a1  ' done

Operation Result:

The output is:

0 1 2 3 4 5 6 7 8 9
Case

The Shell Case statement is a multi-select statement. A case statement can be used to match a value with a pattern, and if the match succeeds, the matching command is executed. The case statement is in the following format:

 Case inch mode 1)    Command1    command2 ...    CommandN    ;; Mode 2)    Command1    command2 ...    CommandN    ;; Esac

The case works as shown above. The value must be followed by the word in, and each pattern must end with a closing parenthesis. The value can be either a variable or a constant. After the match discovery value conforms to a pattern, all commands begin to execute until;;.

The value will detect each pattern that matches. Once the pattern matches, the other mode is no longer resumed after the corresponding command is executed. If there is no matching pattern, use an asterisk * to capture the value and then execute the subsequent command.

The following script prompts you to enter 1 to 4 to match each pattern:

#!/bin/Shecho"Please enter a number from 1 to 4"Readvar Case$var inch     1) echo"you have entered a 1";; 2) echo"you have entered a 2";; 3) echo"you have entered a 3";; 4) echo"you have entered a 4";; *) echo"you are not entering a number from 1 to 4"Esac

Execution Result:

Jump out of the loop

In the loop, sometimes you need to force out of the loop when the loop end condition is not reached, and the shell uses two commands to implement the function: Break and continue.

Break command

The break command allows you to jump out of all loops (all loops after the execution is terminated).

In the following example, the script enters a dead loop until the user enters a number greater than 5. To jump out of this loop and return to the shell prompt, you need to use the break command.

#!/bin/Shstr=Ten while true DoEcho"Please enter a number from 1 to 5:"Read num Case$numinch          1|2|3|4|5) echo"the number entered is $num";; *) echo"no numbers entered from 1 to 5, game over"          Break ;; Esacdone

Execution Result:

Please enter a number from 1 to 5: 1 Enter a number of 1 Please enter a number from 1 to 5: 2 Enter a number of 2 Please enter a number from 1 to 5: 3 Enter a number of 3 Please enter a number from 1 to 5: 4 Enter a number of 4 Please enter a number from 1 to 5: 9 no numbers entered from 1 to 5, game over
Continue

The continue command is similar to the break command, with only a little difference, and it does not jump out of all loops and just jumps out of the current loop.

To modify the above example:

#!/bin/Bash while : DoEcho-N"Enter a number from 1 to 5:"Read Anum Case$aNuminch        1|2|3|4|5) echo"the number you entered is $aNum!"        ;; *) echo"the number you entered is not between 1 and 5!"            ContinueEcho"Game Over"        ;; Esacdone

Running code discovery, when a number greater than 5 is entered, the loop in the example does not end, and the statement echo "Game Over" is never executed.

Esac

The syntax of the case differs greatly from the C family language, which requires a ESAC (which is case, in turn) as the closing tag, each case branch with a closing parenthesis and a two semicolon for break.

Notes:

Typically, the shell variable call needs to be added $, but not in the for (()), as an example:

#!/bin/bashfor ((i=1; i<=5; i++));  Do    "This is the first $i call";d one;     

Execution Result:

This is the 1th call this is the 2nd call this is the 3rd call this is the 4th call this is the 5th call

Similar to C, assignment and next execution can be executed in the loop statement before the code, so note that if you want to do next in the loop body, remember to add $ to the variable, or the program will turn into a dead loop.

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.