What do you use to support command-line arguments for BASH scripts?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
while getopts "hfw:d:m:D:M:r:" OPTION
do
case $OPTION in
h ) help; exit
;;
f ) FORCE=yes
;;
w ) HOMEPAGE=$OPTARG
;;
d ) DOWNLOAD=$OPTARG
;;
m ) MD5SUM=$OPTARG
;;
D ) DOWNLOAD64=$OPTARG
;;
M ) MD5SUM64=$OPTARG
;;
r ) REQUIRES=$OPTARG
;;
* ) help; exit
;;
esac
done
shift $(($OPTIND - 1))
I never got on with getopts: it doesn't handle gnu style long-options, nor options with optional arguments, and from what I've read its use can also differ between implementations.
I usually just code the option parsing manually, no surprises that way.
Last edited by GazL; 06-28-2018 at 12:37 PM.
Reason: expanded reasoning.
I never got on with getopts: it doesn't handle gnu style long-options, nor options with optional arguments, and from what I've read its use can also differ between implementations.
I usually just code the option parsing manually, no surprises that way.
coding the option parsing with case-esac and builtin getopts is hardly shorter than coding it completely manually, and does not seem to have any benefits.
nevertheless i've always been using it, just because it's built into bash.
i should try getopt some day.
Even when allowing for POSIX utility syntax conventions such as option combining it's really not hard to code manually, and as you say, it's not that much more long-winded or complex than the case statements employed by getopt(s).
Here's an example I wrote a few years back that follows -- to the best of my understanding -- all the POSIX guidelines, while also including support for GNU long-options. Not including the help, there are 3 example options (each in both long and short form): one with a mandatory argument, one with an optional argument and one without any argument (a basic flag). It uses no external utilities and no bashisms, just basic shell features.
Code:
#!/bin/sh
# Example of parsing command-line options/arguments.
########################################################################
usage()
{
bs=$(tput bold) ; be=$(tput sgr0)
cat <<-EOF
${bs}SYNOPSIS${be}
options_sh [-v] [-f option_argument] [-l[option_argument]]
[--] [operand...]
options_sh [-?|--help]
${bs}DESCRIPTION${be}
-?
--help
Show this usage information and exit.
-v | --verbose
Basic flag style option: takes no arguments.
-f <argument>
--file=<argument>
Option that requires an argument.
-l[n]
--log[=n]
Option that takes an optional argument (default: n=1 ).
EOF
}
########################################################################
# Parse Option Args:
od_log="1" # Option argument default for "LOG".
unset o_file o_log o_verbose
while [ $# -gt 0 ]
do
case "$1" in
--) shift ; break ;;
-|""|[!-]*) break ;;
-\?|--help) usage ; exit 0 ;;
--file=*) o_file="${1#*=}" ;;
--log) o_log="$od_log" ;;
--log=*) o_log="${1#*=}" ;;
--verbose) o_verbose=1 ;;
--*) echo "unknown long option: $1" >&2 ;;
-?*) # short options:
opts="${1#-}"
while [ "${opts}" ]
do
nopts=${opts#?}
opt=${opts%${nopts}}
case "${opt}" in
v) o_verbose=1 ;;
f) if [ "${nopts}" ]; then
o_file="${nopts}"
nopts=""
else
o_file="${2:?'-f' option expects filename argument!}"
shift
fi
;;
l) if [ "${nopts}" ]; then
o_log="${nopts}"
nopts=""
else
o_log="${od_log}"
fi
;;
?) echo "Invalid option: ${opt}" >&2 ; exit 1 ;;
esac
opts=${nopts}
done
;;
esac
shift
done
########################################################################
# Validate input options/arguments:
# Validate your input here. I find it best to keep parsing and
# validation as two separate activities, otherwise the code
# tends to get unwieldy.
########################################################################
# Rest of script:
[ "$o_verbose" ] && echo "VERBOSE Selected"
[ "$o_file" ] && echo "file: $o_file"
[ "$o_log" ] && echo "LOG Selected, Value is: $o_log"
while [ $# -gt 0 ]
do
echo "Operand: $1"
shift
done
################################################################# End. #
Anyway, fwiw, that's my basic options boilerplate.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.