Shell Selection Statement

Source: Internet
Author: User
Tags case statement

If statement 1) if ... else statement

Syntax for the IF ... else statement:

if [Expression]then    if  is true fi

If expression returns True,then, the statement is executed and no statement is executed if False is returned.


Finally must end with FI to close the if,fi is if the inverted spelling, the back will also meet.

Note: There must be a space between expression and square brackets ([]), or there will be a syntax error.

2) If ... else ... fi statement

Syntax for the If ... else ... fi statement:

if [Expression]then    if  is true Else  If is true fi    

If expression returns true, then the statement behind the then will be executed, otherwise the statement behind the else is executed.

3) If ... elif ... fi statement

The If ... elif. FI statement can judge multiple conditions, with the syntax:

if[Expression1]then Statement (s) to be executedifExpression1  is trueelif [Expression2]then Statement (s) to be executedifExpression2  is trueelif [Expression3]then Statement (s) to be executedifExpression3  is trueElseStatement (s) to be executedifNo expression is truefi

Which expression evaluates to true, executes the statement after which expression is executed, and if it is false, no statement is executed.

Case statement

Case ... Esac is similar to the switch ... case statement in other languages, and is a multi-branch selection structure.

The case statement matches a value or 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    command3    ; Mode 2)    Command1    command2    command3    ;; *)    Command1    command2    command3    ;; Esac

The case works as shown above. The value must be followed by the keyword in, and each pattern must end with a closing parenthesis. The value can be either a variable or a constant. The matching discovery value conforms to a pattern, in which all commands begin to execute until;;.;; Similar to break in other languages, it means jumping to the end of the entire case statement.

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.

1Echo'Input A number between 1 to 4'2Echo'Your number is:\c'3 Read Anum4  Case$aNuminch5     1) echo'You select 1'6     ;;7     2) echo'You select 2'8     ;;9     3) echo'You select 3'Ten     ;; One     4) echo'You Select 4' A     ;; -*) echo'You does not select a number between 1 to 4' -     ;; theEsac

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

Input a number between 1 to 4Your number Is:3you Select 3

  

Shell Selection Statement

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.