Application of getopts in shell

Source: Internet
Author: User

The format of the statement is: Getopts The general format is:

Getopts option_string Variable


Where option_string contains a valid single-character option. If the getopts command finds a hyphen on the command line, it is compared with the word descriptor option_string after the hyphen. If there is a match, set the value of the variable variable to this option

。 If there is no match, then the variable is set to?. When Getopts discovers that there are no characters behind the hyphen, it returns a nonzero status value. The shell program can use the return value of the getopts to create a loop.


Sometimes the option also has a value, and the getopts command also supports this feature. You need to add a colon to the option_string after you select the letter. When the getopts command finds a colon, the value is read from the command line after the option. If the value exists, then there will be a special variable in the Optarg. If the value does not exist, the getopts command holds a question mark in the Optarg and displays a message on the standard error output.


Optstring option string that will match one by one

VarName options for each successful match

ARG parameter list, which takes a command-line argument list when it is not written

$OPTIND special variable, option index, incremented one by one, with an initial value of 1

$OPTARG special variable, option argument, with different values in different cases

Rule 1: When optstring begins with ":", getopts distinguishes between invalid option error and miss option argument error.

Invalid option, the varname will be set to, $OPTARG is the problem of option;
——————————————————————


Here's the actual test:


  [[email protected] mnt]# vim getopt.sh   1 #!/bin/bash                                                                      2 while getopts  "A:B:C: def " what; do  3   case  $what  in  4      a)   5       echo  "this is -a the  arg is !  $OPTARG "     6        ;;   7     B)   8       echo  "This is -b the arg is !  $OPTARG "     9        ;;  10     C)  11       echo  "this  is -c the arg is !  $OPTARG "    12        ;;  13     D)  14         echo   "this is -d the arg is !  $OPTARG"  15          ;;  16     \?)  17       echo  "invalid option: -$OPTARG"      18       ;;  19   esac 20 done

Note A:b:c:def We first verify that the following ": "

A b c is followed by:

[Email protected] mnt]#./getopt.sh-a
./getopt.sh:option requires an argument--a
Invalid option:-

[Email protected] mnt]#./getopt.sh-c
./getopt.sh:option requires an argument-C
Invalid option:-

[Email protected] mnt]#./getopt.sh-c Hello
This is-c, the ARG is! Hello./getopt.sh:option requires an argument-C
Invalid option:-

[[email protected] mnt]#./getopt.sh-b ni
This is-b, the ARG is! Ni

D E F no colon

[Email protected] mnt]#./getopt.sh-d
This is-d, the ARG is!

[Email protected] mnt]#./getopt.sh-d Hello
This is-d, the ARG is!




There are two parameters-A and the following Hello

It can be seen that there is a colon he will force the detection of the following parameters to refer to, no error will be. such as a B C and D does not have: No, he doesn't have a value that the system thinks must be stored in the variable $optarg.


now look at the plot with the first colon

  1 #!/bin/bash                                                                     2  while getopts  ": A:b:c:def"  what; do  3   case  $what  in   4     a)   5       echo  "this is -a the arg is !  $OPTARG"      6        ;;   7     B)   8       echo  "this is -b the arg is !  $OPTARG"      9       ;;  10     C)  11       echo  "this  is -c the arg is !  $OPTARG "    12        ;;  13     D)  14         echo   "this is -d the arg is !  $OPTARG"  15          ;;  16     \?)  17       echo  "invalid option: -$OPTARG"      18       ;;  19   esac 20 done


[Email protected] mnt]#./getopt.sh-a
[Email protected] mnt]#./getopt.sh-b
[Email protected] mnt]#./getopt.sh-c
[Email protected] mnt]#./getopt.sh-d
This is-d, the ARG is!

[Email protected] mnt]#./getopt.sh-a Hello
This is-a, the ARG is! Hello
[Email protected] mnt]#./getopt.sh-b wo
This is-b, the ARG is! Wo

You can see that the system now ignores the following error message-a-b-C would have been to add the parameter value or error, but the head to a: can ignore all error reminders.

There is also a opterr variable if we set it to 0 (default is 1)

Getopts will ignore its own error only to show the system error

Note that the case 1 does not add a parameter value error two

./getopt.sh:option requires an argument-C
Invalid option:-


At this point, we add this to him.

[Email protected] mnt]#./getopt.sh-a
Invalid option:-
[Email protected] mnt]#./getopt.sh-b
Invalid option:-
[Email protected] mnt]#./getopt.sh-c
Invalid option:-
[Email protected] mnt]#./getopt.sh-d
This is-d, the ARG is!
[Email protected] mnt]#/getopt.sh-a 123
This is-a, the ARG is! 123



Application of getopts in 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.