In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
Shell script how to use getopts, I believe that many inexperienced people do not know what to do, so this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
1. Getops
Getopts is a tool for parsing script option parameters.
(1) Syntax format: getopts [option [:]] [DESCPRITION] VARIABLE
(2) option: represents an option that can be used by a script.
(3) ":": if a colon (":") appears after an option (option), it means that this option can be followed by a parameter (that is, a description DESCPRITION).
(4) VARIABLE: save an option in the variable VARIABLE
Getopts is a built-in variable in the linux system and is generally used in loops. Whenever the loop is executed, getopts checks the next command options, and if these options appear in option, they are legal, otherwise they are not. And save these legal options in the variable VARIABLE.
Getopts also contains two built-in variables, OPTARG and OPTIND:
OPTARG stores the parameters (or description information DESCPRITION) after the option in this variable.
OPTIND: this index that represents the next option or argument on the command line (the file name is not counted as an option or argument)
Pay attention to the following points when using for the first time:
The script position parameter will match the individual letters in the optstring one by one. If it matches, it will be assigned to name, otherwise the name will be assigned a question mark.
A single letter in optstring is an option. If the letter is followed by a colon, it means that the option is followed by a parameter, and the parameter value is assigned to the OPTARG variable.
The first one in optstring is a colon, which indicates a masking system error (test.sh: illegal option-- h)
Allow options to be put together, for example-ab
two。 Experiment
(1) getops parameter is easy to use.
#! / bin/bash while getopts': echo "The options is b" echo $OPTARG;; d) echo "The options is d" echo $OPTARG; *) echo "Wrong Options" exit 7;; esac # echo $OPT # echo $OPTARG done echo $OPTIND shift $[$OPTIND-1] echo $1
Execution result:
. / getopts1.sh-d 'nice' fixnale
Description:
When you enter-d, $OPT=d,$OPTARG='nice', therefore displays d). This part of the information.
Since there is an option (- d) and an argument ('nice'), $OPTIND points to the index position of the next option or parameter on the command line, so this is 3.
Shift $[$OPTIND-1] means that the options and parameters in front of the file name are dropped.
(2) further use of the getops parameter
#! / bin/bash echo $* while getopts ": a:bc:" opt do case $opt in a) echo $OPTARG $OPTIND;; b) echo "b $OPTIND";; c) echo "c $OPTIND";;?) Echo "error" exit 1 *; esac done echo $OPTIND shift $(($OPTIND-1)) echo $0 echo $*
Execution result:
. / getopts2.sh-a 11-b-c 6
Description:
The first colon of while getopts ": a:bc:" opt # ignores the error; the colon after the character indicates that the option must have its own parameters.
$optarg stores parameters for the corresponding options, such as 11 and 6 in the example above
$optind always stores the position of the next option to be processed in the original $* (not parameters, but options, in this case, the three options, not the numbers, but the numbers, of course).
Optind initial value is 1, encounter "x", option with no parameters, optind+=1; encounters "x:", options with parameters, optarg=argv [optind+1], optind+=2; encounters "x optind+1:", optional parameters, belong to one of # 1 and # 2.
The first line outputs echo $*
In the second line, the initial value of optind is 1, the parameter of option-an is 11, and the position of the next option to be processed is 3, so the output: 11 3
The third line, and then the next option to be processed by-b-c position is 4, so output: B4
The fourth line, and then-c has parameters, so the next position to be processed is + 2, so output: C 6
After reading the above, have you mastered how to use getopts in the Shell script? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.