Linux case command in Shell

Source: Internet
Author: User
Tags continue

The case name in the shell under Linux is similar to the switch in C + +, but the cases in the shell are more powerful and complex.

1, strong mainly reflected in: Shell in case the keyword can be a string type, and each item can contain regular expressions.

2, the complexity is mainly reflected in: the shell of each item in the case after three choices: Break (regular break), unconditional follow up (unconditional continue) and conditional follow up (conditional continue).

The main point of this article is the 2nd above.

The unconditional continuation of the case and conditional continuation are features that are available in the Bash 4.1.x and later versions.

Execute the following command to view the version of your current bash

Echo $BASH _version

Regular break is added at the back of each item;;

To continue unconditionally is to add;& after each item

Conditionally continue to be added after 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

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.