A concise tutorial on Shell script case statements _linux Shell

Source: Internet
Author: User
Tags case statement

The Shell Case statement is a multiple-selection statement. You can use a case statement to match a value with a pattern, and if the match succeeds, execute the matching command. The case statement is formatted as follows:

Copy Code code as follows:

Case value in
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 a variable or a constant. When a match finds a value that conforms to a pattern, all commands begin to execute until;

The value will detect each pattern that matches. Once the pattern matches, the corresponding command after the match pattern is executed and no more mode continues. If there is no matching pattern, use the asterisk * to capture the value, and then execute the following command.

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

Copy Code code as follows:

Echo ' Input a number between 1 to 4 '
Echo ' Your number is:\c '
Read Anum
Case $aNum in
1) echo ' you select 1 '
;;
2) echo ' You select 2 '
;;
3) echo ' you select 3 '
;;
4) echo ' You select 4 '
;;
*) echo ' You don't select a number between 1 to 4 '
;;
Esac

Input different content, there will be different results, such as:
Input a number between 1 to 4
Your number Is:3
you select 3

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.