008-shell Process Control

Source: Internet
Author: User

First, if else1.1, if

If statement syntax format:

if condition  Then     command1    command2     ... fi

Write a line (for the terminal command prompt):

if [$ (PSgrep"ssh"1thenecho"  True"fi

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

1.2. If Else

If ELSE syntax format:

if condition  Then     command1    command2    ... CommandNElse    commandfi
1.2. If else-if else

If else-if else syntax format:

if Condition1  Then     command1elifthen     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"Else   Echo "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    echo' two numbers equal! ' Else    Echo ' two numbers are not equal! ' fi

Output Result:

Two numbers equal!
Second, for Loop

2.1. For loop General format:

 for inch item1 item2 ... itemn  Do     command1    command2    ... CommandN Done

Write a line:

 for inch  Do 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    Echo " The value is: $loop "  Done

Output Result:

12 3)45

Characters in the sequential output string:

 for inch ' This is a string '  do    Echo $str  Done

Output Result:

String

General use

#!/bin/bashfor ((i=1; i<=5; i++));  do    Echo " This is the first $i call " ; done;

Output

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
third, 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 viewed in detail: 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>.

Echo ' Press <CTRL-D> Exit ' Echo '  ' while read FILMdo    echo' 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
Four, Infinite cycle

Infinite loop Syntax format:

 while  :  Do     commanddone or whiletrue    command  Done or  for (( ; ; ))

V. 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=0until  ]do   echo  $a   a= 'expr1' done

Operation Result:

The output is:

0 1 2 3 4 5 6 7 8 9

Vi. 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

Vii. jumping 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 : Do    Echo-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 ;; Esac Done

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 : Do    Echo-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"        ;; Esac Done

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.

Eight, 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.

008-shell Process Control

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.