Shell condition judgment

Source: Internet
Author: User

Shell condition judgment
1. if condition judgment: if [condition]; then do something fi multiple conditions: & represents AND | represents OR multiple judgments: if [condition]; then do something elif [condition 2]; then // do something else // do someshing fi

#!/bin/bashread -p "Please input (Y/N) : " ynif [ "$yn" == "Y" ] || [ "$yn" == "y" ]; then     echo "Yes!"elif [ "$yn" == "N" ] || [ "$yn" == "n" ]; then     echo "No!"else    echo "Input Error"fiexit 0
View running results
[work@www sh]$ sh hello.sh Please input (Y/N) : yYes![work@www sh]$ sh hello.sh Please input (Y/N) : lInput Error[work@www sh]$
2. case judgment if there are many judgment levels in if, it is recommended to use case, otherwise there are too many nesting, and the code is prone to deterioration. Case $ variable name in "variable 1") do something; each end uses two semicolons (variable 2) do something ;;*) the last * indicates other values do something; esac:
#!/bin/bashcase $1 in    "one")        echo "One"        ;;    "two")        echo "Two"        ;;    *)        echo "Usage $0 (one|two)"        ;;esacexit 0
Running result
[work@ww sh]$ sh hello.sh Usage hello.sh (one|two)[work@www sh]$ sh hello.sh oneOne[work@www sh]$
Address: http://blog.csdn.net/yonggang7/article/details/40480695

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.