Shell command getopts Usage

Source: Internet
Author: User

Write shell scripts Common sh test.sh

The notation of-M 2-d 3

Case script:

#!/bin/bashwhile getopts ": a:b:c:" Arg #选项后面的冒号表示该选项需要参数doecho  "arg": $arg case        $arg in             a)                echo "a ' s ARG: $OPTARG---$OPTIND "#参数存在 $OPTARG                ;;             b)                echo "B ' s arg: $OPTARG---  $OPTIND"                ;;             c)                echo "C ' s ARG: $OPTARG---$OPTIND";;     ?)  #当有不认识的选项的时候arg为?            echo "unkonw argument"        exit 1        ;;        Esacdone

The getopts consists of two constants:

$OPTARG: Save the value of the current option

$OPTIND: The displacement of the parameter list, the initial value is 1, the index value of the next parameter

Getopts ": a:b:c:": The first colon is the error message is ignored, the last colon is the last parameter can be obtained, the argument is an empty move index gets the value of the last parameter

Shell command getopts Usage

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.