How does Shell obtain parameters?

Source: Internet
Author: User

 

How does Shell obtain parameters?

When writing a shell script, you often need to obtain parameters from the outside, for example:

$ sh demo.sh a

So how can we upload the above parameter A to the shell program.
$0 $1... $10, where $0 is the name of the shell program, $1, $2... they are the first parameter and the second parameter... our demo. SH:

$ cat demo.sh#! /bin/bashcat << EOF     Usage: sh $0 $1EOF$ sh demo.sh a Usage: sh demo.sh a

Parameters starting with $ include:

$0: program name
$ #: Number of variables
[Email protected]: All variables, namely, $1... $, excluding the program name.

This is a problem. When many parameters are used by others in your program, the interaction is not very good. Others have to remember the location and role of each parameter in the script, which will be very troublesome. You can add a prompt, for example

$ sh demo.sh -a A -b B -c C 

How can this be implemented? There are two ways to achieve this:shiftAndgetopts

$ cat demo.sh #! /bin/bashwhile [ $# -gt 0 ];do   case $1 in   -a) echo "-a----$2"       shift       ;;   -b) echo "-b----$2"       shift       ;;   -c) echo "-c----$2"       shift       ;;   esac   shiftdone$ sh demo.sh -a A -b B -c C-a----A-b----B-c----C

getoptsThe function simplifies the processing of the preceding options. Let's take a look at its usage.
The first parameter of the function: a string of valid option letters ·:The parameter must be provided.getoptsThe function places the parameters inOPTARGThe other variable isOPTINDIndicates the index value of the next parameter to be processed.
The second parameter of the function: variable name.getoptsWhen called, the variable name is updated.getoptsWhen a valid option is not found, this variable is set as a question mark. The specific implementation is as follows:

#! /Bin/bashwhile getopts A: B: C: OPT; docase $ opt INA) echo "-A ---- $ optarg"; B) echo "-B ---- $ optarg ";; c) echo "-C ---- $ optarg ";;?) Echo "$ OPT is an invalid option"; esacdone $ sh demo. sh-a-B-c-d-a ---- a-B ---- B-c ---- cdemo. SH: Illegal option -- d -- error message output by getopts? Is an invalid option -- the script determines the output error message. Here we set-D option?

However, getopts does not support the long option.getoptYou canmanBelow:

Name
Getopt-Parse Command Options (enhanced)

It can be seen from the basic explanation that it is an enhanced versiongetopts, AlthoughgetoptsIt has more than one second, but the power of theory is far inferior.getopt.

 

 

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.