The shell getopts and getopt usage 1.getoptsgetopts (shell built-in command) cannot directly handle long options (such as:--prefix=/home, etc.), Getopts has two parameters, the first parameter is a string, including characters and ":", each character is a valid option if the character is followed by ":", indicating that the character has its own arguments. Getopts takes these parameters from the command and deletes the "-" and assigns it to the second parameter, and if it has its own argument, the parameter is assigned in "Optarg". The shell that provides getopts has built in the variables $optarg store the parameters of the corresponding options, and $optind always stores the next element position to be processed in the original $*. While getopts ": A:BC" opt #第一个冒号表示忽略错误; the colon after the character indicates that the option must have its own parameters. Example: #cat getopts.sh#!/bin/bashecho $* while getopts ": A:BC" O PT do case $opt in a) echo $optarg echo $optind;; b) echo "B $optind";; c) echo "C $optind";; ? ) echo "Error" exit 1;; Esac done echo $optind shift $ (($optind-1)) #通过shift $ (($optind-1)) processing, only the parameters that remove the contents of the option are retained in $*, which can then be processed by normal shell programming. echo $ echo $* execute command:./getopts.sh-a 11-b-c-a 11-b-C 3 B 4 C 5 5./getopts.sh2.getoptgetopt (an external tool) specific use can be #man Getopt#-o represents a short option, two colons indicates that the option has an optional parameter, the optional parameter must be close to the option, such as-carg instead of the-C arg#--long to indicate the long option example: #cat getopt.sh#!/bin/bash# a small Example Program for UsinG The new Getopt (1) program. # This program would work with Bash (1) # A similar program using the TCSH (1) script. Language can found # as PARSE.TCSH # example input and output (from the bash prompt): #./parse.bash-a par1 ' anothe R arg '--c-long ' wow!*\? '-cmore-b ' Very long ' # option A # option C, no argument # option C, argument ' more ' # opt Ion B, argument ' very long ' # remaining arguments: #--' par1 ' #--' Another arg ' #--' wow!*\? ' # Note that the We use ' "[email protected]" ' to let each command-line parameter expand to a # separate word. The quotes around ' [email protected] ' is essential! # We need temp as the ' eval set--' would nuke the return value of getopt. #-o represents a short option, two colons indicates that the option has an optional parameter, the optional parameter must be close to the option #如-carg instead of the-c arg #--long means the long option # "[email protected]" explained above #-N: The information on error #-- : Give an example to understand: #我们要创建一个名字为 "-F" directory you will do? # mkdir-f #不成功, because-f is parsed by mkdir as an option, you can use # mkdir---F so-F will not be used as an option. Temp= ' Getopt-o ab:c::--long a-long, B-long:,c-long:: \ n ' example.bash '--"[email protected]" ' If [$?! = 0]; Then echo "Terminating ..." >&2 exit 1fi # Note the quotes around ' $temp ': they is essential! #会将符合getopt参数规则的参数摆在前面, the others are in the back, and in the last side add-eval set--"$temp" #经过getopt的处理, the specific options are processed below. While Truedo case "$" in-a|--a-long) echo "option A" shift;; -b|--b-long) echo "option B, argument \ ' $ '" Shift 2;; -c|--c-long) Case "$" in "" "echo" option C, no argument "Shift 2;; *) echo "option C, argument \ ' $ '" Shift 2;; ESAC;; --) shift break;; *) echo "Internal error!" Exit 1;; Esacdone echo "remaining arguments:" For Arg do echo '--' "\ ' $arg '"; Done Run command:./getopt.sh--b-long abc-a-c33 remain option B, argument ' ABC ' option A option C, argument ' remaining ' Arguments:--' remain '
Record Getopts and getopt usages in the shell