Shell Process Control

Source: Internet
Author: User
Tags case statement closing tag

Shell Process Control

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 syntax format:

if conditionthen    command1    command2    ... CommandNElse    Commandfi

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:

A=Tenb= -if[$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"there are no conditions to meet"fi

Output Result:

A is less than B

The If Else statement is often used in conjunction with the Test command, as follows:

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

Output Result:

Two numbers equal!

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:

 for inch 1 2 3 4 5  Do  "The value is: $loop" done     

Output Result:

 is 1  is 2is 3are 4is  5

Characters in the sequential output string:

 for inch ' This is a string '  Do     Echo $strdone

Output Result:

 is string

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/shint=1while (($int<=5  )) does     echo $int "int++" Done    

Run script, Output:

1 2 3 4 5

Using the Bash let command in use, which executes one or more expressions, the variable calculation does not need to add $ to represent the variable, which can be consulted: Bash lets command

The while loop can be used to read keyboard information. In the following example, the input information is set to the variable film, ending the loop by <Ctrl-D>.

' Press <CTRL-D> Exit '    "while readFILMdo     " Oh, yes! $FILM is a good site " Done

Run the script with the output similar to the following:

Press <CTRL-D> Exit enter your favorite site name: Beginner's Tutorial Yes! Rookie Tutorial is a good site

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

The condition can be any test condition, and the test takes place at the end of the loop, so the loop executes at least once-note this.

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:

Echo'Enter a number from 1 to 4:'Echo'the number you entered is:'Read Anum Case$aNuminch    1) echo'you chose 1.'    ;; 2) echo'you chose 2.'    ;; 3) echo'you chose 3.'    ;; 4) echo'you chose 4.'    ;; *) echo'you didn't enter a number from 1 to 4.'    ;; Esac

Enter different content, there will be different results, for example:

1 4 number between: The number you entered is: 3  3

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/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! Game over"             Break        ;; Esacdone

Execute the above code and the output is:

1 Number between 5 :33!  numbers between 15 :715 ! 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 is over!" will never be 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.

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.