How to use the getopts of shell scripts

Source: Internet
Author: User

Encounter shell script need to collect parameter scene, with Getopts very convenient


getopts optstring name [arg ...] Example: Getopts "A:fs" OPTION


Command Description:


optstring lists all the parameters that the corresponding shell script can recognize. For example, if the Shell script can recognize the-a,-f and the-s parameter, then optstring is AFS, the default can not be followed by the value, if the corresponding parameter followed by a value, then the corresponding optstring followed by a colon.

For example, A:FS indicates that a value appears after a parameter, in the form of type-a.

In addition, when Getopts performs a match to a, the value is stored in a shell variable called Optarg.

If Optstring is preceded by a colon, a parameter in the command business that does not appear in the optstring will not prompt the error message. For example, an undefined-X does not report "illegal option-X"

The name represents the names of the parameters, and each time the getopts is executed, the next parameter is fetched from the command business and then stored in name. If the obtained parameter is not listed in optstring, the value of name is set to?.

All parameters in the command business have an index, the first parameter starts at 1, and so on. There is also a shell variable named Optind that holds the index of the next parameter to be processed.

Note: The parameters of the optstring can be passed all in the script, or not passed in



1.getopts allows options to be stacked together (e.g.-fs)


2. If the parameter is to be taken, the corresponding option should be added after: (such as a after a parameter a:fs). At this point the options and parameters are separated by at least one white space character, so options cannot be stacked.


3. $optind always store the position of the next element (not the parameter, but the option) in the original $*;


Optind initial value is 1, encountered "X", option without parameters, optind + = 1; encountered "x:", with parameter options, Optind + = 2; encountered "x::", optional parameters, belongs to one of the # # and # #, GNU extension implementation.

The position of the-a parameter in the example above is 1, because there is a parameter 11 behind it, the next parameter that needs to be processed after-B is positioned at 3, which is known as the output

3; The next parameter to be processed is-s, with a value of 4,-s followed by parameter +2, and the parameter to be processed after IS position 6.


4, the use of getopts processing parameters, although convenient, but there are still two small limitations:

A. The format of the option parameter must be-D Val, not a-dval with no spaces in the middle.

B. All option parameters must be written in front of other parameters, because getopts is processed from the front of the command line, encounters a non-starting argument, or an option parameter end tag-aborts, and if a non-option command-line argument is encountered in the middle, then the option parameters are not taken.

C. Do not support long options, that is, options such as--debug

A parameter built into bash by getopts, if you want to use a more powerful feature, you can use another separate command under the system getopt


Cat getopts.sh

#!/bin/bashecho [email protected]   parameter list while getopts   "A:BC:"  optdo     case  $opt  in a)             echo  "a  $OPTARG   $OPTIND"             ;;          B)             echo  "b   $OPTIND"   #-b do not follow the parameters, otherwise encountered non-option command line arguments, will end             ;;          C)             echo  "c  $OPTARG   $OPTIND"             ;;          ?)             #没有的参数, will be stored in the?            echo  "Error"          exit       esacdoneecho  $OPTIND   #下一个要处理的参数的位置号shift  $ ((  $OPTIND -2 )    #移除前4个echo  $0    #脚本名echo  $*    #参数列表-------------a 11  -b -c 6a 11 3b  4c 6 66./getopts.sh6




This article from "Small Fish Blog" blog, declined reprint!

How to use the getopts of shell scripts

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.