Shell Programming: The Ultimate Beginner's Guide to Learn Shell Step by Step by Kathleen Peterson

Shell Programming: The Ultimate Beginner's Guide to Learn Shell Step by Step by Kathleen Peterson

Author:Kathleen Peterson [Peterson, Kathleen]
Language: eng
Format: azw3, epub, pdf
Published: 2020-10-07T00:00:00+00:00


Page 165

3 parameters and arguments

164

To take care of such options, you can use the arguments of the command

dozeile either check and evaluate it yourself or use the command

topts. Here is the syntax to getopts:

getopts options variable [arguments]

To use the getopts function correctly, you must observe the following rules:

Even if topts is used, this does not require that optimal

must be specified.

Options must precede the file name or parameter:

command -o file.txt

# Right

command file.txt -o

# Not correct

The order in which the options are specified is not important:

command -o -x file.txt # Correct

command -x -o file.txt # Also correct

Options can also be summarized:

command -ox file.txt # Correct

command -xo file.txt # Correct

If getopts is called, this command checks whether an option (initiated

with a »-«) in the position parameters (from left to right). Of the

The parameter to be edited next is stored in the auto-

matic variable OPTIND managed. The value of this variable is when called

initially 1, but is increased by 1 with every further getopts call. When a

The command line is to be read in several times, the index must be returned manually.

be set.

If an option is found, this character is assigned to the variable »Variable«

grasslands. In the string »Options« all switches are marked with an identification book

letters indicated. At switches that receive additional arguments, a

Colon appended. The options that expect another argument (i.e.

those marked with a colon) are stored in the shell variable OPTARG

saved.

Unless an "argument" is explicitly passed when calling getopts,

the shell command uses the positional parameters, i.e. the arguments of

the command line of the script.

If getopts returns 0, this indicates that an option has been found. At a

Value not equal to 0, the end of the arguments has been reached or an error has occurred.

to step. If you want to avoid outputting an error message, you can use a double

Use pelpunkt as the first character or set the shell variable OPTERR to 0.

3

Usually getopts is evaluated in a loop with a case construction.

Both have not yet been dealt with (we will only come to the next chapter

tel on this topic), but we don't want to talk about the

leave dry theory. Here is a possible example:

# Demonstrated topped

# Name: agetopter

while getopts abc: D: opt

do

case $ opt in

a) echo "Option a" ;;

b) echo "Option b" ;;

c) echo "Option c: ($ OPTARG)" ;;

D) echo "Option D: ($ OPTARG)" ;;

esac

done

The script when running:

you @ host> ./agetopter -a

Option a

you @ host> ./agetopter -b

Option b

you @ host> ./agetopter -c

./agetopter: option requires an argument - c

you @ host> ./agetopter -c Hello

Option c: (hello)

you @ host> ./agetopter -D

./agetopter: option requires an argument - D

you @ host> ./agetopter -D Again

Option D: (Again)

you @ host> ./agetopter -ab

Option a

Option b

you @ host> ./agetopter -abD

Option a

Option b

./agetopter: option requires an argument - D

you @ host> ./agetopter -abD Hello

Option a

Option b

Option D: (Hello)

you @ host> ./agetopter -x

./agetopter: illegal option - x

In the example you could also see the effects of the colon

recognize behind an option symbol. Give when using such

If the option does not take any further arguments, an error message is returned. Self

a wrong argument evaluates getopts here and reports the error.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.