Search This Blog

Friday, May 24, 2013

shell script with options

This is an example of using getopts in a shell script. Compare it to the getopt example.

# Example illustrating use of getopts builtin. This
# shell script would implement the paste command,
# using getopts to process options, if the underlying
# functionality was embedded in hypothetical utilities
# hpaste and vpaste, which perform horizontal and
# vertical pasting respectively.
paste=vpaste # default is vertical pasting
seplist="\t" # default separator is tab

while getopts d:s o
do case "$o" in
d) seplist="$OPTARG";;
s) paste=hpaste;;
[?]) print >&2 "Usage: $0 [-s] [-d seplist] file ..."
exit 1;;
shift $OPTIND-1

# perform actual paste command
$paste -d "$seplist" "$@"

No comments: