Using getopts to process input parameters in the shell

Source: Internet
Author: User

In writing shell scripts, we often have to deal with some input parameters, it is more convenient to find getopts in the process, and can handle the parameters and parameter values of user input very well.

Getopts is used to process user input parameters, with examples of how to use: while GETOPTS:A:B:CDEFG opt;   Do case $opts in a) do sth;    .... cde) do another; Esacdone Several important variables: Optind: Getopts uses Optind as an index to handle the next parameter to be processed, recording the current state. Optarg: in the above loop, a A, a, a, a, a, a, a two parameter has a colon, the colon indicates that the input parameter is followed by a parameter value, when getopts found a colon, the user input parameter value is processed, this parameter is stored in the Optarg. Optstring: In the example above: a:b:cdefg,getopts the parameters to be processed. Attention The first colon ":" is used to designate getopts to work in silent mode,In silent mode, when a user enters a parameter that does not meet optstring, the error message illegal option is not printed, making the code look more professional. If you want to work in verbose mode, you can remove the first colon below and simply write a shell script that describes how to use the getopts: #! /bin/bash
function C1 () {
Cmd="RHC app Create-p Redhat"
whileGetopts: A: t: SN opt; Do
Case$opt inch
a) cmd= $cmd"-a $OPTARG";;
T) cmd= $cmd"-t $OPTARG";;
s) cmd= $cmd"-S";;
N) cmd= $cmd"-N--no-dns";;
\?) echo"Invalid param";;
Esac
Done
Echo $cmd
}

C1 -A App1 -t perl-5. Ten-S- n Execute this script and we'll get the expected results [[email protected] shell]#./getopts-silent.sh
RHC app Create -P redhat -a App1 -t perl-5. Ten-S- n --no-dns Of course, there are times when We update the network to write the function to the. BASHRC, it is convenient to call at any time, if you paste this code directly into the. BASHRC, may cause problems: I did not get the expected results, the CMD in the script is not handled as expected, this is because of what? The reason for this is that, after the first execution, after the completion of the. BASHRC, the next time it is executed, the Optind will not be re-generated because he is used as a global variable, so when Getopts is called, his index becomes chaotic. The reason why you do not encounter this problem in the script is also here, because each time the script executes, a new shell is called, so the Optind is set to 1. If you want him to take effect in. BASHRC, you must add the top Local OptindLet's change it a little bit: # Create Apps
function Create-apps () {
Local Optind
Cmd="RHC app create-p $OPENSHIFT _passwd"
whileGetopts A: t: SN x
Do
Case$x inch
a) cmd= $cmd"-a $OPTARG";;
T) cmd= $cmd"-t $OPTARG";;
s) cmd= $cmd"-S";;
N) cmd= $cmd"-N--no-dns";;
\?) echo Invalid Params;;
Esac
Done
Echo $cmd
}After source, we can call directly to see if we have reached the desired result:
[Email protected] shell]# create-apps-a free-t jbsseap---P redhat-a free-t jbsseap-n--no-dns

If you are not familiar with this, you can simply look at this example, if the parameters passed in include F, then execute the corresponding code:

 while " dfiprrvw " opt      //assign to opt      do                    case in F)                       //If passed in is F                "[email protected]"                ;;             *)                              do nothing       //pass other parameters, including d,i,p,r,v,w                ;;        Esac       Done

Using getopts to process input parameters in the shell

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.