Case commands in shell in Linux (regular break, unconditional continuation, and conditional continuation)

Source: Internet
Author: User

Case commands in shell in Linux (regular break, unconditional continuation, and conditional continuation)

In linux, the case name in shell is similar to the switch in C/C ++, but the case in shell is more powerful and complex.

1. The power mainly lies in the fact that the keywords in case in shell can be strings, and each item can contain regular expressions.

2. complexity is mainly reflected in the following three options after each item of case in shell: break (conventional break) and unconditional follow up (unconditional continuation) and conditional follow up (conditional continuation ).

This article focuses on the above 2nd points.

The unconditional continuation of case and conditional continuation are only available in bash 4.1.x and later versions.

Run the following command to view your current bash version.

echo $BASH_VERSION

The conventional break is added after each item ;;

Unconditional continuation is added after each item ;&

Conditional addition of each item ;;&

Test code

#!/bin/bashecho "Test No.1 ..."case "1" in    1)        echo '1'        ;;&    2)        echo '2'        ;;    3)        echo '3'        ;;    ?)        echo '?'        ;;    *)        echo '*'        ;;esacecho "Test No.2 ..."case "1" in    1)        echo '1'        ;;    2)        echo '2'        ;;&    3)        echo '3'        ;;    ?)        echo '?'        ;;    *)        echo '*'        ;;esacecho "Test No.3 ..."case "1" in    1)        echo '1'        ;&    2)        echo '2'        ;;    3)        echo '3'        ;;    ?)        echo '?'        ;;    *)        echo '*'        ;;esacecho "Test No.4 ..."case "1" in    1)        echo '1'        ;;    2)        echo '2'        ;&    3)        echo '3'        ;;    ?)        echo '?'        ;;    *)        echo '*'        ;;esac
Test results:
Test No.1 ...1?Test No.2 ...1Test No.3 ...12Test No.4 ...1

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.