Case usage in shell

Source: Internet
Author: User
Case Statement in shell: it can match the variable content with multiple templates, and then decide which part of the code should be executed based on the template that matches successfully. Format of use for www.2cto.com: case matching mother board in template 1 [| template 2] & hellip;) statement group ;;...
Case Statement in shell: it can match the variable content with multiple templates, and then decide which part of the code should be executed based on the template that matches successfully. Format of www.2cto.com: case matching mother board in template 1 [| template 2]…) Statement group; template 3 [| Template 4]…) Statement group; the matching sequence of esaccase statements is from top to bottom. Therefore, the principle of case statement writing is from top to bottom, and the template is from special to common. In C, the case statement contains the default template. in shell programming, the template may be written as * to complete the same function. Www.2cto.com routine :#! /Bin/sh echo "Please input \" yes \ "or \" no \ "" read var case "$ var" in [yY] [eE] [sS]) echo "Your input is YES"; [nN] [oO]) echo "Your input is no"; *) echo "Input Error! "; Esac exit 0 Example 2 :#! /Bin/bashFRUIT = $1 # note that there is no space if [-z "$1"] # There must be a space in brackets and-z thenecho "there is no fruit input in the first parameter" elsecase" $ FRUIT "inapple | APPLE) echo "the fruit is apple"; banana | BANANA) echo "the frult is banana"; # you can use *) to indicate THE default options, note that we should end with two semicolons *) echo "is the input fruit I want? "; Esacfi #### $1 is the value of an external parameter, for example, when the script is run :. /test. sh apple #### here, apple is $1 ___________________________________________________________ another script to be learned #! /Bin/bash HOSTS = "nss. sh cs. sh SS. sh ds_handler2 tomcat blog httpd "for myHost in $ HOSTSdocount = ('PS aux | grep-v grep | grep $ myHost | wc-L') echo $ myHostecho $ countdone
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.