When writing a shell program, you often need to handle command-line arguments, and this article describes the command-line approach under bash.
Options and Parameters:
Like the following command line:
Copy Code code as follows:
./test.sh-f Config.conf-v--prefix=/home
-F is an option, it requires a parameter, that is, config.conf,-V is also an option, but it does not require parameters.
--prefix We call it a long option, the option itself is more than one character, it also requires a parameter, with an equal sign connection, of course, the equal sign is not necessary,/home can be written directly behind the--prefix, that is,--prefix/home, more restrictions later specifically mentioned.
In bash, command line arguments can be handled in three ways, each with its own scenario.
* Manual Handling mode
* Getopts
* getopt
In turn, the three methods of processing are discussed.
1, hand-handled mode
In the manual process, you first need to know a few variables, or the above command behavior example:
Copy Code code as follows:
* $:/test.sh, which is the command itself, corresponds to the argv[0 in C + +
* $: F, first parameter.
* $2:config.conf
* $, $ ... Analogy
* Number of $# parameters, excluding the command itself, $ #为4 in the example above.
* $@: A list of the parameters themselves, and does not include the command itself, as the above example is-f config.conf-v--prefix=/home
* $*: Same as $@, but "$*" and "$@" (quoted) and different, "$*" interprets all parameters as a string, and "$@" is an array of arguments.
Example:
Copy Code code as follows:
#!/bin/bash
For ARG in "$*"
Todo
Echo $arg
Done
For ARG in "$@"
Todo
Echo $arg
Done
Execution./test.sh-f Config.conf-n 10 will print:
-F config.conf-n #这是 "$*" output
-F #以下为 $@ output
Config.conf
-N
10
So, the way of manual processing is to deal with these variables. Because manual processing is highly dependent on the position of the parameters you pass on the command line, it is generally used only to handle simpler parameters.
(Script Academy Www.jb51.net Edit the collation)
For example:
./test.sh 10
and rarely use./test-n 10 this option with options. Typical uses are:
Copy Code code as follows:
#!/bin/bash
If [x$1!= x]
Then
#... With parameters
Else
Then
#... No parameters
Fi
Why use x$1!= x as a way to compare? Imagine comparing this way:
If [-N $] #$1 not empty
However, if the user does not pass the argument, it is empty, and then it becomes [-n], so you need to add a secondary string to compare.
Manual processing can satisfy most of the simple requirements, with shift use can also build a powerful function, but in the process of dealing with complex options to recommend the following two methods.
2. getopts/getopt
Handling command-line arguments is a similar and complex thing, and for this reason C provides functions such as Getopt/getopt_long,
Boost from C + + provides the options library, and in the shell, it's getopts and getopt.
The getopts and getopt functions are similar but not exactly the same, where Getopt is a standalone executable and getopts is built by bash.
Let's take a look at the typical usage of parameter passing:
Copy Code code as follows:
*./test.sh-a-b-c: Short options, no parameters required for each option
*./TEST.SH-ABC: Short options, like the previous method, just write all the options together.
*./test.sh-a Args-b-C: Short option, where-a requires parameters, and-b-c does not require parameters.
*./test.sh--a-long=args--b-long: Long option
First look at getopts, it does not support long options.
Using getopts is very simple:
Copy Code code as follows:
#test. Sh
#!/bin/bash
While getopts "A:BC" Arg #选项后面的冒号表示该选项需要参数
Todo
Case $arg in
A
echo "A ' s arg: $optarg" #参数存在 $optarg
b
echo "B"
C
echo "C"
?) #当有不认识的选项的时候arg为?
echo "Unkonw argument"
Exit 1
Esac
Done
You can now use:
./test.sh-a Arg-b-C
Or
./test.sh-a ARG-BC
To load up.
It should be said that most scripts use this function, and if you need to support long options and optional parameters, then you need to use getopt.
Getopt An example of a self band:
Copy Code code as follows:
#!/bin/bash
# A small example program for using the ' new Getopt (1) program.
# This program'll only work with Bash (1)
# an similar program using the TCSH (1) script language can is found
# as Parse.tcsh
# example input and output (from the bash prompt):
#./parse.bash-a par1 ' Another arg '--c-long ' wow!*\ '-cmore-b ' very long '
# Option A
# option C, no argument
# option C, argument ' more '
# option B, argument ' very long '
# remaining arguments:
#--> ' Par1 '
#--> ' another arg '
#--> ' wow!*\? '
# This we use ' "$@" ' to let each command-line parameter expand to a
# separate word. The quotes around ' $@ ' are essential!
# We need temp as the ' eval set-' would nuke ' return value of getopt.
#-o represents a short option, and a two colon indicates that the option has an optional parameter, and optional arguments must be pressed close to the option
#如-carg instead of-c ARG
#--long represents long option
# "$@" is explained above.
#-N: Information when an error occurs
#--To give an example of better understanding:
#我们要创建一个名字为 "-F" directory What are you going to do?
# mkdir-f #不成功, because F is parsed by mkdir as an option, you can use the
# mkdir---F so-F will not be taken as an option.
Temp= ' Getopt-o ab:c::--long a-long,b-long:,c-long:: \
-N ' example.bash '--"$@"
If [$?!= 0]; Then echo "Terminating ..." >&2; Exit 1; Fi
# Note the quotes around ' $temp ': they are essential!
#set rearrange the order of the parameters, which is to change the value of the $1,$2 ... $n, these values are rearranged in the getopt.
Eval set--"$temp"
#经过getopt的处理, the following handles the specific options.
While true; Todo
Case "$" in
-a|--a-long) echo "option A"; shift;;
-b|--b-long) echo "option B, argument \ ' $"; Shift 2;;
-c|--c-long)
# C has an optional argument. As we are in quoted mode,
# an empty parameter would be generated if its optional
# argument is not found.
Case "$" in
"") echo "option C, no argument"; Shift 2;;
*) echo "option C, argument \ ' $ '"; Shift 2;;
ESAC;;
-) shift; break;;
*) echo "Internal error!"; Exit 1;;
Esac
Done
echo "Remaining arguments:"
For Arg do
Echo '--> ', ' $arg ';
Done
such as using
./test-a-B Arg arg1-c
As you can see, the command line has an extra arg1 parameter, and after Getopt and set, the command lines become:
-a-b Arg-c--arg1
The arg1 pointing to the-a,$2 pointing to the-b,$3 pointing to the arg,$4 pointing to the--, is placed at the end.
3, Summary
General Small Script manual processing is enough, getopts can handle most of the situation, getopt more complex, more powerful functions.