Processing Line Options

Andrew Mallett AUTHOR AND CONSULTANT @theurbanpenguin www.theurbanpenguin.com

Module Overview Inputting data using command line arguments Processing arguments using shift Processing options to scripts $ ./myscript.sh fred jones staff $0 $1 $2 $3 $# = 3 $0 = ./myscript.sh $* = "fred jones staff" $@ = ("fred" "jones" "staff")

Script Variables When passing arguments to scripts they populate variables. $0 represents the script itself, $# is the number of arguments, the complete arguments listed can be shown with $* as single string or stored in a an array $@. $ script.sh "The script is: %s\n" "$0" printf "The number of arguments is: %d\n" "$#" printf "The arguments list is: %s\n" "$*" printf "The arguments as an array are: %s\n" "$@"

Demonstration Script Creating the script allows us to see the special variables in use, especially the difference between $* and $@.

Demo

Let's investigate the use of arguments in scripts. Shift The builtin shift command is used to move arguments along. Having processed $1 we can move $2 to become $1 using the shift command. This is most useful in a loop allowing us to work only with $1 where the argument list can be or any size. $ cat shift.sh fname="$1" shift lname="$1" printf "First: %s Last: %s\n" "$fname" "$lname"

Simple Shift Script As a simple illustration we can see how this script only requires us to deal with $1. Practically, it is less useful as we know there must be two arguments for each element of the name. $ cat shift1.sh while (( "$#" )) do "$1" shift done

While Loop Using Shift A way to iterate all arguments provided to the script can use of shift within a while loop. The loop will continue while the argument count is above 0.

Demo

Processing arguments with shift. getopts The builtin command getopts allows for options to be passed to your script. We can create a script to create or delete users; the correct action coming from the option -c to create and -d to delete. Using Getopts

while getopts ':c:d:' opt do case "$opt" in c) sudo useradd -m "$OPTARG" break ;; d) sudo userdel -r "$OPTARG" break ;; *) echo "Usage: $0 [-c|-d] " ;; esac done getopts -cd getopts c:d -c fred -d getopts c:d: -c fred -d joe getopts :cd -h

Require Option Arguments The colon following an option required an argument to be supplied to the option. A colon prepending the list of options allows the default error handling to be overwritten. Custom error handling is managed by the case else action. $ getopts.sh -c -- fred $ getopts.sh -d -- fred

End-of-options It can be necessary to separate option arguments from script arguments. If we wanted the user to be processed as $1 and drop the requirement for the option argument we could make use of the -- in the script execution.

Demo

Working with script options.

Demo

Working with end-of-options.

Script Arguments - $0: script name - $1: first argument Summary - $#: number or arguments - $*: all arguments as a string - $@: all arguments in an array - shift: moves arguments Using Options: - getopts 'cd' - getopts ':cd' - getops 'cd:' - getopts':c:d:' Next up: Working With