getopts Handling Shell Script parameters

Source: Internet
Author: User

  1. Intruduction

    Shell scripts have two ways to locate script parameters, one is to use positional variables, and the second is to use getopts. There are two limitations to using positional parameters, and he needs the programmer to test the error himself and establish the corresponding message. If you use shift to manipulate parameters, the shift command will remove all parameters and will not be possible if you want to access them again later.

    Getopts is the built-in command, which makes it easy to resolve command-line positional parameters to options and verify that the options are valid.


  2. getopts optstring name [args]


    Three environment variables referenced by getopts

    optarg: The value of the option parameter that was processed by the getopts built-in command, option argument (hold option parameter), the getopts command puts it in the variable optarg when an option parameter is required for the selected item

    optind: The next sequence number of parameters to be processed by the getopts built-in command, option Index, which will be incremented by the initial value of 1 for each invocation of the script.

    opterr: If set to 1,bash, getopts error will be displayed. Set to 0, getopts error is not displayed.


    The process of getopts

    When the script is invoked, Optind is initialized to 1. Each time getopts is called, the next option value is assigned to name, the option index value Optind also points to the next location to process the option, and the option parameter is assigned to Optarg

    Getopts 's design goal is to run in a loop, each time it executes,getopts checks the next command-line argument and determines if it is valid. (That is, check whether the parameter starts with -- followed by a letter contained in the opstring).

    Valid, the matching option letter exists in the specified variable variable , and returns the exit status 0 (ture);

    Invalid ( if - The following letter is not included in the options), it is stored in the variable . , and returns exit status 0If no arguments are already in the command line, or if the next parameter does not start with - the exit status of 0 is returned (false, which can be used to end the while Cycle ).


    Getopts after all parameters are processed, a non-0 value (False, Exit loop) is returned, at which point the Optind index value points to the first non-option parameter [args],name?


    Getopts Error Handling

    When the command is used correctly, name is used to store option, $OPTARG the parameter used to hold option. If the command is entered incorrectly (invalid option, missing parameter),getopts will handle illegal option error and Miss option argument error. The processing result differs from whether the opstring begins with: Opstring: The error message that is used to mask getopts processing (the same effect can be achieved by placing opterr in 0 in the script).


    Use the following test case getopts.sh to quickly verify how Getopts handles the error

    Opstring value ": Ab:c:" and "AB:C:"

    ./getopts.sh-a-B ok-c No

    ./getopts.sh-a-B

    ./getopts.sh-w



    commonly used in scripts ? catch error,Name,optarg treats the value of the error can be used to define the output of the error message itself.


    Example
    Opstring
    Type of error
    Name
    Optarg
    ./getopts.sh-a-B
    " : ab:c: "
    Missoption argument
    :
    B
    ./getopts.sh-w
    " : ab:c: "
    Illegal options
    ?
    W
    ./getopts.sh-a-B
    "Ab:c:"
    Missoption argument
    ?
    Unsetoptarg
    ./getopts.sh-w
    "Ab:c:"
    Illegal options
    ?
    unset Optarg (cancel variable )
  3. Special Instructions

    : ? and is not used as a choice character for special purposes.

    Getopts allows options to be stacked together (e.g.-ms)

    Opstring contains an option string that can be put together, if the option is followed by a parameter that is required in the call of the option, the invocation of such an option is not stacked with other options.

    The same shell environment executes getopts multiple times, the optind is not reset, and when the parameter is recalled, the Optind must be reset manually if necessary.

    Getopts returns 0 (TRUE) when a defined or undefined option is found; Returns a value other than 0 (false) if the parameter is processed or if an error is encountered


  4. eg

Cat getopts.sh


#!/bin/bash

#===============================================================================

#

# FILE:getopts.sh

#

# USAGE:./getops.sh

#

# Description_:

#

# OPTIONS:---

# REQUIREMENTS:---

# BUGS:---

# NOTES:---

# Author:adelphos (), [email protected]

# Company:

# version:1.0

# created:11/06/2014 04:15:10 AM EST

# REVISION:---

#===============================================================================

While getopts "AB:C:" opt

Do

Case ${opt} in

a) echo-e "\ r

Opt:\t $opt

Optind: \ t $OPTIND \ r

Optarg: \t$optarg \ r

Opterr: \ t $OPTERR ";;

b) Echo-e "\ r

Opt:\t $opt

Optind: \ t $OPTIND \ r

Optarg: \t$optarg \ r

Opterr: \ t $OPTERR ";;

c) Echo-e "\ r

Opt:\t $opt

Optind: \ t $OPTIND \ r

Optarg: \t$optarg \ r

Opterr: \ t $OPTERR ";;

?) echo "Usage:"

Echo-e "\t-a Specify the installation path"

Echo-e "\t-b Specify the product type which you want to install, possible Choiceis ese,consv,clent"

Echo-e "\t-c Specify the InstallPackage"

echo-e "\ r

Opt:\t $opt

Optind: \ t $OPTIND \ r

Optarg: \t$optarg \ r

Opterr: \ t $OPTERR ";;

Esac

Done




This article is from the "Adelphos" blog, make sure to keep this source http://adelphos.blog.51cto.com/2363901/1573965

getopts Handling Shell Script parameters

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.