Bash getopts parsing Options | Shell Script

Shell Scripting

The options to a shell script, single characters preceded by a hyphen, can be parsed with the built-in command getopts. There may be arguments to some options, and options must precede nonoption arguments.

Multiple options may be concatenated with a single hyphen, but any that take an argument must be the final option in the string. Its argument follows, with or without intervening whitespace.

On the following command line, there are two options, -a and -f. The latter takes a file name

argument. John is the first nonoption argument, and -x is not an option because it comes after a nonoption argument.

myscript -a -f filename John -x Jane

The syntax for getopts is as follows:

getopts OPTSTRING var

The OPTSTRING contains all the option’s characters; those that take arguments are followed by a colon. For the script in Listing 4-3, the string is f:v. Each option is placed in the variable $var, and the option’s argument, if any, is placed in $OPTARG.

Usually used as the condition to a while loop, getopts returns successfully until it has parsed all the options on the command line or until it encounters the word –. All remaining words on the command line are arguments passed to the main part of the script.

A frequently used option is -v to turn on verbose mode, which displays more than the default

information about the running of the script. Other options—for example, -f—require a file name argument.

This sample script processes both the -v and -f options and, when in verbose mode, displays some information.

Listing 4-3. parseopts, Parse Command-Line Options

progname=${0##*/} ## Get the name of the script without its path

## Default values verbose=0 filename=

## List of options the program will accept;

## those options that take arguments are followed by a colon optstring=f:v

## The loop calls getopts until there are no more options on the command line

## Each option is stored in $opt, any option arguments are stored in OPTARG

while getopts $optstring opt do

case $opt in

f) filename=$OPTARG ;; ## $OPTARG contains the argument to the option v) verbose=$(( $verbose + 1 )) ;;

*) exit 1 ;;

esac done

## Remove options from the command line

## $OPTIND points to the next, unparsed argument shift “$(( $OPTIND – 1 ))”

## Check whether a filename was entered if [ -n “$filename” ]

then

if [ $verbose -gt 0 ]

then

printf “Filename is %s\n” “$filename”

fi else

if [ $verbose -gt 0 ]

then

printf “No filename entered\n” >&2 fi

exit 1

fi

## Check whether file exists if [ -f “$filename” ]

then

if [ $verbose -gt 0 ]

then

printf “Filename %s found\n” “$filename”

fi

else

if [ $verbose -gt 0 ]

then

printf “File, %s, does not exist\n” “$filename” >&2

fi

exit 2 fi

## If the verbose option is selected,

## print the number of arguments remaining on the command line if [ $verbose -gt 0 ]

then

printf “Number of arguments is %d\n” “$#”

fi

Running the script without any arguments does nothing except generate a failing return code:

$ parseopts

$ echo $?

1

With the verbose option, it prints an error message as well:

$ parseopts -v

No filename entered

$ echo $?

1

With an illegal option (that is, one that is not in $optstring), the shell prints an error message:

$ parseopts -x

/home/ram/bin/parseopts: illegal option – x

If a file name is entered and the file doesn’t exist, it produces this:

$ parseopts -vf qwerty; echo $? Filename is qwerty

File, qwerty, does not exist

2

To allow a nonoption argument to begin with a hyphen, the options can be explicitly ended with –:

$ parseopts -vf ~/.bashrc – -x Filename is /home/ram/.bashrc Filename /home/ram/.bashrc found Number of arguments is 1

In case of any ©Copyright or missing credits issue please check CopyRights page for faster resolutions.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.