Bash Getopts-Allow your script to support command line parameters
In the past, I always wanted to know how to create command line parameters for my Bash script. After searching, I found two functions that can solve this problem: the getopt function
Getopt/getopts: command line option/parameter processing in Bash 0. When writing a program, you must always process command line parameters. This article describes the command line Processing Method in Bash. Options and parameters: for example, the
IntruductionShell scripts have two ways to locate script parameters, one is to use positional variables, and the second is to use getopts. There are two limitations to using positional parameters, and he needs the programmer to test the error
The Bash shell provides a number of different ways to get data from users, including the following 3 methods:
Command line arguments (data added in the back of fame)
command-line Options (single letter with modifiable command behavior)
Encounter shell script need to collect parameter scene, with Getopts very convenientgetopts optstring name [arg ...] Example: Getopts "A:fs" OPTIONCommand Description:optstring lists all the parameters that the corresponding shell script can
To become a flexible tool, a qualified script must provide additional information to describe the role of the script, how it is executed, and where it is executed. Like the command, the script also uses parameters. Switches and parameters increase
1. Introduction to getopts
Thanks to the flexibility of shell command lines, write your ownCodeWhen judging, the complexity is relatively high. Use the Internal Command getopts to easily process command line parameters. The general format is:
The getopts parsing command line parameter getopts command is built in shell. you can get the valid command line parameters specified by a single character. each character has a & amp; lsquo; -& amp; rsquo; No. or & amp; lsquo; + & amp; rsquo; No.
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
Focus on Listing 7Now, many Linux® and UNIX® systems have bash shells, which are the common default shells on Linux. In this article, you'll learn how to handle parameters and options in a bash script, and how to use the shell's parameter extensions
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.