The getopts of Linux

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;

Esac

Done

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, b two parameters each have a colon, the colon indicates that the input parameter is followed by a parameter value, when the colon is found getopts, will process the user input parameter values, this parameter value is saved in Optarg.

Optstring: In the example above: a:b:cdefg,getopts the parameters to be processed. Note that the first colon ":" is used to specify that the getopts works in silent mode, in silent mode, when the user enters a parameter that does not meet the optstring, does not say illegal option to print out the error message, making the code look more professional. If you want to work in verbose mode, you can remove the front colon

Below, simply write a shell script to describe how getopts is used:

#! /bin/bash
function C1 () {
Cmd= "RHC app create-p RedHat"
While GETOPTS:A:T:SN opt; Do
Case $opt in
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.10-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.10-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 local optind to the top

Let's change it a little bit:



# Create Apps
function Create-apps () {
Local Optind
Cmd= "RHC app create-p $OPENSHIFT _passwd"
While getopts A:T:SN x
Do
Case $x in
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-n
RHC app create-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 getopts "dfiprrvw" opt//Assign to opt
Do
Case $opt in
f)//If passed in is F
exec $realrm "[Email protected]"
;;
*)

# do nothing//pass other parameters, including d,i,p,r,v,w
;;
Esac
Done

The getopts of Linux

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.