Shell Script Programming Case Statement learn notes _linux Shell

Source: Internet
Author: User
Tags case statement

Case statements are used to implement multiple if. else, but the usage is somewhat different, people who have learned other languages know that the case to match the variables, if there is a matching successful words to execute the corresponding statement. The case statement in Shell programming is also this meaning, cases will match the string, starting from the first mode, if a pattern has been matched successfully, the other mode will not be matched, the following look at the use of the code.

#!/bin/sh
echo "Please yes or no"
read input

#case语句的基本用法, remember that each match has a double semicolon behind it, representing the end of this pattern and the beginning of the next # pattern, Do not add double quotes when matching wildcard characters "
$input" in
Yes) echo "yes";;
y*) echo "y*";;
Y) echo "y";;
NO) echo "no";;
n) echo "n";;
*) echo "default";;
Esac

#合并匹配模式, case matching can only match one, want to match multiple results using the following way case
"$input" in
y* | y*) echo "yes";;
n | N) echo "no";;
*) echo "default";;
Esac

#一种更屌的匹配 Case
"$input" in
[Yy][ee][ss] | [YY]) echo "yes";;
[NN] | [NN] [OO]) echo "no";;
*) echo "default"
echo "End"
Esac

exit 0
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.