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 and the getopts function. I have no intention of arguing which 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 next command line: [vb]. /test. sh-f config. conf-v -- prefix =/home we call-f as the option. It requires a paramete
of the script.opterr: If set to 1,bash, getopts error will be displayed. Set to 0, getopts error is not displayed.The process of getoptsWhen the script is invoked, Optind is initialized to 1. Each time getopts is called, the next option value is assigned to name, the option index value Optind also points to the next l
Getopts is a bash shell built-in command that parses a command-line pass in a shell script, passes it to a function, or passes it to a shell script that is passed to another call (option or parameter, which is explained later, getopts only supports short options, To resolve the long option, refer to getopt).getopts com
Tags: loop while test ble tar ons share picture image explanationThe 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) main getopt, getopts command
Ability to read input directly from the keyboard
13.2-Opti
it, the next parameter that needs to be processed after-B is positioned at 3, which is known as the output3; The next parameter to be processed is-s, with a value of 4,-s followed by parameter +2, and the parameter to be processed after IS position 6.4, the use of getopts processing parameters, although convenient, but there are still two small limitations:A. The format of the option parameter must be-D Val, not a-dval with no spaces in the middle.B.
that will match one by oneVarName options for each successful matchARG parameter list, which takes a command-line argument list when it is not written$OPTIND special variable, option index, incremented one by one, with an initial value of 1$OPTARG special variable, option argument, with different values in different casesRule 1: When optstring begins with ":", getopts distinguishes between invalid option error and miss option argument error.Invalid o
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
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:
Getopts options variable
Getopts is designed to run in a loop. Each tim
":" is added before each switch for the specified item s, m, d, and p, a parameter is required for this toggle. For details, see the code my_shell.sh. it implements the function of converting all parameters such as daily hours, minutes, and seconds to seconds: click (here) fold or open SECS = 0 MINUTES = 0 HOURS = 0 DAYS = 0 PROCESS = 0 while getopts: s: m: h: d: p TIMED 2>/dev/NullDC case $ TIMED in s) SECS = $ OPTARG; m) (MINUTES = $ OPTARG * 60);
by Francis_hao Jul 5,2017Getopts is a built-in command for the shell.OverviewGetopts optstringname [args]Optind,optarg,opterrDescribeGetopts is used by the shell program to analyze positional parameters,optstring contains the option characters that need to be recognized, if the character followed by a colon, indicating that the word check requires a parameter, the parameters need to be separated by a space. Colons and question marks cannot be used as option characters.Getopts each time it is cal
I. Introduction of GETOPTSBecause of the flexibility of the shell command line, the complexity is higher when you write code judgments. Command-line arguments can be easily handled using the internal command getopts. The General format is:Getopts Options VariableGetopts is designed to run in a loop, and each time a loop is executed, getopts checks the next command-line argument and determines if it is legit
getopts CommandUseProcesses command-line arguments and verifies valid options.Grammargetopts option string name [parameters ...]DescribeGetopts is designed to run in a loop, and each time a loop is executed, getopts checks the next command-line argument and determines if it is legitimate. That is, check whether the parameter starts with a--followed by a letter contained in the options. If it is, put the mat
It is necessary to specify the value of the command line option in the script. Getopts provides a way to place a colon after the option_string option, for example:
Getopts abcsd: Option
ABCs can be passed without adding actual parameter values, and option D must contain parameters. The variable optarg saves the value, the variable optind is used to store the next element location in the original $.
#!
In writing shell scripts, we often have to deal with some input parameters, it is more convenient to find getopts in the process, and can handle the parameters and parameter values of user input very well.Getopts is used to process user input parameters, with examples of how to use:While GETOPTS:A:B:CDEFG opt; DoCase $opts ina) do sth;......CDE) do another;EsacDoneSeveral important variables:Optind:getopts uses Optind as an index to handle the next pa
[Shell] before using getopts in a function. bashrc has written a function that uses getopts, but it is strange that after this function is called once, the parameter cannot be parsed again and then queried, the OPTIND variable is detected. The original code is as follows: www.2cto. c... [shell] before using getopts in a function. bashrc has written a function tha
In writing shell scripts, we often have to deal with some input parameters, it is more convenient to find getopts in the process, and can handle the parameters and parameter values of user input very well.Getopts is used to process user input parameters, with examples of how to use: while GETOPTS:A:B:CDEFG opt; Do case $opts in a) do sth; .... cde) do another; Esacdone Several important variables: Optind:Getopts uses Optind as an index to handle the n
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 argsIf the script.sh parameter is-f filename-e editor-p Cat, that is./script.sh-f filename-e editor-p CatThe options and parameters can be judged as follows:While
Transferred from: http://yejinxin.github.io/parse-shell-options-with-getopts-command/Standard UNIX commands generally offer a number of options, with the user providing specific options and parameters, in the form of a command line, as follows:command -options parameters filenameGetopts is a shell built-in command that helps us to handle command-line options so that our scripts can be consistent with UNIX commands. The usage format for
#!/bin/bashwhile getopts I:VH Namedo case $name in i) Opt=1 echo $OPTARG;; V) opt=2 Echo 2;; h) opt=3 echo 3;; *) echo "Invalid arg-$OPTARG";; EsacdoneThe getopts is used in the following form: getopts option_string variableGetopts There are two parameters, the first is the-I option, and the second parameter is a
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.