Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand linux shell command line options and parameter usage

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to understand linux shell command line options and parameter usage". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to understand linux shell command line options and parameter usage".

Problem description: how to handle command line options such as tail-n 10 access.log in linux shell?

In bash, you can handle command-line arguments in three ways, each with its own application scenario.

1, deal with it directly, parse $1 and 2 respectively, and handle it by hand

2Get getopts to deal with single character options (e. G.:-n 10-f file.txt, etc.)

3Get getopt, which can handle either single character option or long option long-option (such as:-- prefix=/home, etc.).

Summary: small script can be handled by hand, getopts can handle the vast majority of cases, getopt is more complex and more powerful.

1. Direct manual processing of position parameters

You have to know several variables.

The code is as follows:

* $0: that is, the command itself, which is equivalent to the argv [0] in cCompact +.

* $1: the first parameter.

* $2, $3, $4.: parameters 2, 3, 4, and so on.

* number of $# parameters, excluding the command itself

* $@: a list of the parameters themselves, excluding the command itself

* $*: same as $@, but unlike "$*" and "$@" (in quotation marks), "$*" interprets all parameters as a string, while "$@" is an array of parameters.

Manual processing can meet most simple requirements, and powerful functions can be constructed with shift, but the following two methods are recommended when dealing with complex options.

Example, (getargs.sh):

The code is as follows:

#! / bin/bash

If [$#-lt 1]; then

Echo "error.. need args"

Exit 1

Fi

Echo "commond is $0"

Echo "args are:"

For arg in "$@"

Do

Echo $arg

Done

Run the command:

The code is as follows:

. / getargs.sh 11 22 cc

Commond is. / getargs.sh

Args are:

eleven

twenty-two

Cc

2 shell getopts (built-in command)

Handling command-line arguments is a similar and complex matter. For this reason, c provides functions such as getopt/getopt_long, and C++ 's boost provides the options library. In shell, it is getopts and getopt that handle this.

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 (such as:-- prefix=/home, etc.)

About how to use getopts, you can search getopts on man bash.

Getopts has two parameters. The first parameter is a string, including the character and ":". Each character is a valid option. If the character is followed by a ":", it means that the character has its own parameter. Getopts takes these parameters from the command and deletes the "-" and assigns it to the second parameter, which is assigned in "optarg" if it has its own parameter. The shell that provides getopts has a built-in change called optarg, and getopts modifies this variable.

Here the variable $optarg stores the parameters of the corresponding option, while $optind always stores the location of the next element to be processed in the original $*.

While getopts ": a:bc" opt # the first colon indicates ignoring the error; the colon after the character indicates that the option must have its own parameters

Example, (getopts.sh):

The code is as follows:

Echo $*

While getopts ": a:bc" opt

Do

Case $opt in

A) echo $optarg

Echo $optind

B) echo "b $optind"

C) echo "c $optind"

?) Echo "error"

Exit 1

Esac

Done

Echo $optind

Shift $(($optind-1))

# through the processing of shift $(($optind-1)), only the parameters that remove the contents of the option are retained in $*, which can be processed by normal shell programming later.

Echo $0

Echo $*

Execute the command:

The code is as follows:

. / getopts.sh-a 11-b-c

-a 11-b-c

eleven

three

B 4

C 5

five

. / getopts.sh

3Grammer getopt (an external tool)

Specific usage can be man getopt

#-o indicates a short option, and two colons indicate that the option has an optional parameter, which must be close to the option, such as-carg instead of-carg

#-long indicates 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 will only work with bash (1)

# an 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 arg'-- c-long 'wowned *\?'-cmore-b "very long"

# option a

# option c, no argument

# option c, argument `more'

# option b, argument `very long'

# remaining arguments:

#-- > `par1'

#-- > `another arg'

#-- > `wowboys *\?'

# note that 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 the return value of getopt.

#-o indicates a short option, and two colons indicate that the option has an optional parameter, which must be close to the option

# such as-carg but not-carg

#-long indicates long option

# "$@" explained above

#-n: information in case of error

# -: it is easier to understand by giving an example:

# what will you do if we create a directory named "- f"?

# mkdir-f # is not successful because-f will be parsed by mkdir as an option, so you can use the

# mkdir-- f so that-f will not be used as an option.

Temp= `getopt-o ab:c::-- long Amuri longline bmuri longvus recorder cmurlongizuo:\

-n 'example.bash'-- "$@" `

If [$?! = 0]; then echo "terminating..." > & 2; exit 1; fi

# note the quotes around `$temp': they are essential!

# set rearranges the order of the parameters, that is, changing the value of $1, which has been rearranged in getopt.

Eval set-"$temp"

# after the processing of getopt, the specific options are dealt with below.

While true; do

Case "$1" in

-a |-a-long) echo "option a"; shift

-b |-- b-long) echo "option b, argument\ `$ 2'"; shift 2

-c |-- c-long)

# c has an optional argument. As we are in quoted mode

# an empty parameter will be generated if its optional

# argument is not found.

Case "$2" in

"") echo "option c, no argument"; shift 2

*) echo "option c, argument\ `$ 2'"; shift 2

Esac

--) shift; break

*) echo "internal error!"; exit 1

Esac

Done

Echo "remaining arguments:"

For arg do

Echo'-->'"\ `$ arg'"

Done

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 `33'

Remaining arguments:

-- > `remain'

Thank you for reading, the above is the content of "how to understand linux shell command line options and parameter usage". After the study of this article, I believe you have a deeper understanding of how to understand the usage of linux shell command line options and parameters, and the specific usage needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report