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 bash shell command line options and fix incoming parameter handling

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

Share

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

This article focuses on "how to understand bash shell command line options and fix incoming parameter handling". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to understand bash shell command line options and fix incoming parameter handling"!

When writing shell programs, it is often necessary to deal with command-line parameters. This paper describes the command-line processing method under bash.

Options and parameters:

The following command line:

The code is as follows:

. / test.sh-f config.conf-v-- prefix=/home

-f is an option, it requires a parameter, that is, config.conf, and-v is also an option, but it does not require an argument.

-- prefix we call it a long option, that is, the option itself is more than one character, and it also requires a parameter, which is connected with an equal sign. Of course, the equal sign is not required. / home can be written directly after-- prefix, that is,-- prefix/home. More restrictions will be discussed later.

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

* manual processing

* getopts

* getopt

The three processing methods are discussed in turn.

1. Manual processing.

In manual processing, you first need to know a few variables, or do you want to use the above command behavior example:

The code is as follows:

* $0:. / test.sh, that is, the command itself, is equivalent to the argv in cswab croup + [0]

* $1:-f, the first parameter.

* $2: config.conf

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

The number of * $# parameters, excluding the command itself. In the above example, $# is 4.

* $@: the list of parameters themselves, excluding the command itself, such as-f config.conf-v-- prefix=/home in the above example

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

Example:

The code is as follows:

#! / bin/bash

For arg in "$*"

Do

Echo $arg

Done

For arg in "$@"

Do

Echo $arg

Done

Execute. / test.sh-f config.conf-n 10 to print:

-f config.conf-n 10 # this is the output of "$*"

-f # below is the output of $@

Config.conf

-n

ten

Therefore, the way of manual processing is the processing of these variables. Because manual processing is highly dependent on the position of the parameters you pass on the command line, it is generally only used to deal with simpler parameters.

(edited and organized by script School www.yisu.com)

For example:

. / test.sh 10

The option of. / test-n 10 is rarely used. Typical usage is:

The code is as follows:

#! / bin/bash

If [Xero1! = x]

Then

#... Have parameters

Else

Then

#... No parameters

Fi

Why would you compare it in such a way as xmatch 1! = x? Imagine this way of comparing:

If [- n $1] # $1 is not empty

However, if the user does not pass a parameter, $1 is empty, which becomes [- n], so you need to add an auxiliary string for comparison.

Manual processing can meet most simple requirements, and it can also be used with shift to construct powerful features, but the following two methods are recommended when dealing with complex options.

2. Getopts/getopt

Dealing with command line arguments is a similar and complex thing, for which c provides functions such as getopt/getopt_long

C++ 's boost provides the options library, and in shell, it is getopts and getopt.

Getopts and getopt are similar but not identical in function, where getopt is a separate executable and getopts is built-in by bash.

Let's take a look at the typical use of parameter passing:

The code is as follows:

*. / test.sh-a-b-c: short options, no parameters are required for each option

*. / test.sh-abc: short options, which have the same effect as the previous method, but write all the options together.

*. / test.sh-an args-b-c: short option, where-a requires parameters and-b-c does not.

*. / test.sh-- a-long=args-- b-long: long option

Let's take a look at getopts, which does not support the long option.

Using getopts is very simple:

The code is as follows:

# test.sh

#! / bin/bash

The colon after the while getopts "a:bc" arg # option indicates that the option requires an argument

Do

Case $arg in

a)

The echo "asides arg:$optarg" # parameter is stored in $optarg

b)

Echo "b"

c)

Echo "c"

?) # what is arg when there is an unrecognized option?

Echo "unkonw argument"

Exit 1

Esac

Done

You can use it now:

. / test.sh-an arg-b-c

Or

. / test.sh-an arg-bc

To load.

It should be said that most scripts use this function, and if you need to support long options and optional parameters, you need to use getopt.

An example that comes with getopt:

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

For example, using the

. / test-a-b arg arg1-c

You can see that the command line has an extra arg1 parameter, and after going through getopt and set, the command line becomes:

-a-b arg-c-- arg1

$1 points to-a recording 2 points to-b recording 3 points to arg,$4, points to-c recording 5 points to--, while the extra arg1 is put at the end.

At this point, I believe you have a deeper understanding of "how to understand bash shell command line options and fix incoming parameter handling". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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