Shell Programming--getopts Usage Summary

Source: Internet
Author: User

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

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.