Shell scripts or function-passed arguments, you can access positional variables to use the values of the parameters. However, in the case of complex parameters, it is best to use getopts processing, which handles the command line options and parameters.
Syntax format:
Getopts option_string args
If the script.sh parameter is-f filename-e editor-p Cat, that is./script.sh-f filename-e editor-p Cat
The options and parameters can be judged as follows:
While Getopts f:e:p:option
Do
Case ' ${option} ' in
F
File_name=${optarg};;
E
Editor=${optarg};;
P
Page=${optarg};;
\?)
command;;
Esac
Done
Option_string contains the option characters that are handled, such as F,e,p. If there is a colon after an option that indicates the option to specify a parameter, space between options and arguments, such as-f filename
The option is saved in args, such as the example option, when the F option is processed, the value of the $option is F, the parameter value is saved in Optarg, and the F option is $OPTARG the value is filename.
Getopts after each successful processing option, the exit status is 0
When the getopts command returns a value other than 0, no option is processed and the while loop exits.
If there is a colon after an option, indicating that the option is to specify parameters, and if the script is executed with no arguments, Getopts assigns the value to option. The special character, when used as is, needs to be escaped.
This article is from the "11317177" blog, please be sure to keep this source http://11327177.blog.51cto.com/11317177/1787413
Shell Programming--getopts Usage Summary