This article describes the use of command-line options and command-line arguments in a Linux shell, and in bash, you can use the following three ways to handle commands
Row parameters, each of which has its own application scenario
Question Description: How to handle command-line options such as Tail-n Access.log in the Linux shell.
In bash, command line arguments can be handled in three ways, each with its own scenario.
1, direct processing, in turn to the $1,$2,..., $n, respectively, hand-handled;
2,getopts to handle the case of a single character option (such as:-N 10-f file.txt options);
3,getopt, you can handle a single character option, or you can handle long options long-option (such as--prefix=/home, etc.).
Summary: Small script manual processing can, getopts can handle most of the situation, getopt more complex, more powerful function.
1, directly handle position parameters manually
You have to know a few variables,
The code is as follows:
* $: The command itself, equivalent to the argv[0 in C + + +]
* : The first parameter.
* $, $ $, $ ... : 2nd, 3, 4 parameters, and so on.
* number of $# parameters, excluding the command itself
* $@: A list of the parameters themselves, also excluding the command itself
* $*: And $@ the same, but "$*" and "$@" (quotes) and different, "$*" Interpret all parameters as a string, and "$@"
is an array of arguments.
Manual processing can meet the majority of simple requirements, with shift use can also build a powerful function, but the processing of complex options recommended by the following
Two methods.
example, (getargs.sh):
The code is as follows:
#!/bin/bash
If [$#-lt 1]; then
echo "error. Need args "
exit 1
fi
echo" Commond is $ "
echo" args are: "For
arg in ' $@ '
do
echo $ar G Done
To run the command:
The code is as follows:
/getargs.sh cc
Commond is./getargs.sh
args are:
cc
2,getopts (Shell built-in command)
Handling command-line arguments is a similar and complex thing, and for this reason, C provides functions such as Getopt/getopt_long, which boost provides
The options library, in the shell, deals with getopts and getopt.
The difference between getopts/getopt is that getopt is an external binary file, and getopts is shell builtin.
The code is as follows:
[Root@jbxue ~]$ type getopt
getopt is/usr/bin/getopt
[root@jbxue ~]$ type getopts
getopts is a shell builtin
Getopts cannot directly handle long options (e.g.--prefix=/home, etc.)
For getopts use, you can search for a man bash getopts
Getopts has two parameters, the first argument is a string, including the character and ":", each character is a valid option if
The character is followed by ":" to indicate that the character has its own arguments. Getopts gets these parameters from the command and deletes the "-", and
Assign it to the second argument, and if it has its own argument, the parameter is assigned to "Optarg". Provides a getopts shell built in
Optarg This change, getopts modifies the variable.
Here the variable $optarg stores the parameters of the corresponding option, and $optind always stores the next element position to be processed in the original $*.
While getopts ": A:BC" opt #第一个冒号表示忽略错误 the colon after the character indicates that the option must have its own argument
example, (getopts.sh):
The code is as follows:
echo $* while
getopts ": A:BC" opt does case
$opt in
a) echo $optarg
echo $optind;;
(b) echo "B $optind"
(c) echo "C $optind";
echo "Error"
exit 1;;
Esac
do
echo $optind
shift $ (($optind-1))
#通过shift the processing of $ ($optind-1), where only the parameters that remove the contents of the option are retained. Normal shell programming can then be
processed.
echo $
echo $*
To execute a command:
Copy code code as follows:
./getopts.sh-a 11-b-C-a
11-b
-C-one
3
b 4
C 5
5
/getopts.sh
3,getopt (an external tool)
The specific use can be man getopt
#-o represents a short option, and a two colon indicates that the option has an optional parameter, and the optional parameter must be pressed close to the option, such as-carg and not-c ARG
#--long represents long option
example, (getopt.sh):
The code is as follows:
#!/bin/bash # A small example program for using the new Getopt (1) program. # This program would only have work with Bash (1) # A similar program using the TCSH (1) script. Language can be found # as PARSE.TCSH # example input and output (from the bash prompt): #./parse.bash-a Par1 ' another A RG '--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!*\ ' ' $@ ' 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, a two colon indicates that the option has an optional parameter, and the optional parameter must be close to the option #如-carg instead of the-c arg #--long the long option # "$@" explained above #-N: Information when an error #----to give an example is better understood: #我们要创建一个名字
Directory for "-F" what are you going to do?
# mkdir-f #不成功, because-F is resolved by mkdir as an option, then the # mkdir can be used---f so that-F is not 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 values of the $1,$2 $n, which rearrange the eval set--"$temp" #经过getopt的处理 in Getopt, and deal with the specific options below. While true; Do 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, # A empty parameter'll 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
To run the command:
The code is as follows:
./getopt.sh--b-long abc-a-c33 remain
option B, argument ' abc '
option a
option C, argument ' Remainin '
G arguments:
--> ' remain '