getopts called in function not picking up flags [duplicate] - bash

This question already has answers here:
Why does getopts only work the first time?
(2 answers)
Closed 3 years ago.
I have the following script that I call from my .bash_profile:
# Set directories based on current path
__set_dirs() {
currdir=`pwd`
if [[ ${currdir} =~ "\/path\/to\/main\/(.*)\/working\/([a-z]+)(/?.*)" ]]
then
ws=${BASH_REMATCH[1]}
subdirs=${BASH_REMATCH[3]}
stgdir=${ts}/${ws}/STAGING${subdirs}
else
echo "${currdir} is not a workspace"
stgdir=""
fi
}
# Update local version with staging version
upd() {
__set_dirs
if [[ -n ${stgdir} ]]
then
__overwrite=0
while getopts "o" opt
do
case $opt in
o)
__overwrite=1
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
esac
done
echo "Updating ${currdir} with latest from ${stgdir}..."
if [ ${__overwrite} -eq 0 ]
then
update -r ${stgdir} ${currdir}
else
echo "Overwriting all local changes!"
update -r -w ${stgdir} ${currdir}
fi
fi
unset __overwrite
}
When I execute
> upd -o
The flag is completely ignored--I never see the message "Overwriting all local changes!". Have I missed something somewhere?
UPDATE: It does work, but only the first time I run the script. From the second time on, the flag gets ignored.

All right, figured it out:
After rifling through the man page for getopts, I found this tidbit (emphasis mine):
Each time it is invoked, getopts places...the index of the next argument to be
processed into the variable OPTIND. OPTIND is initialized to 1 each
time the shell or a shell script is invoked.... The shell does not
reset OPTIND automatically; it must be manually reset between
multiple calls to getopts within the same shell invocation if a new set of parameters is to be used.
Since I only run the script once from by .bashrc, OPTIND only gets initialized once. The first time I run the function, everything's hunky dory. The second time on, OPTIND is set to 2 and getopts doesn't find anything there, so it moves on.
Armed with this knowledge, I modified upd() to reset OPTIND to 1:
upd() {
__set_dirs
if [[ -n ${stgdir} ]]
then
__overwrite=0
OPTIND=1
while getopts "o" opt
...
That fixed it. OPTIND: more important than you'd think.

getopts acts on the parameters passed into your function, so in your case you have to call the upd() function with "$#", to pass all the command line parameters into your function.
eg:
test() {
while getopts "o" opt; do
case $opt in
o)
echo o
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
esac
done
}
test # this wont work as $1, $2, ... are not set
test $# # this will work as you pass along the command line parameters
Edit
I overlooked the .bashrc part, if I source the example above, into my running shell, then test -o works as expected.

Related

Bash - Best way to have helpFunction & default parameter values

After a lot of read, SO or others, I'm really wondering about the best / cleanest way to have a bash script with parameters, optionals with default values.
Here is my script for now:
#!/bin/bash
helpFunction()
{
echo ""
echo "Usage: $0 --reload --mode=[single|cluster]"
echo -e "\t--reload Reload the database : fixtures & schema"
echo -e "\t--mode Mode of build : single or cluster"
exit 1
}
while [[ "$#" -gt 0 ]]; do
case $1 in
-h|--help) helpFunction; shift ;;
-r|--reload) reload=true; shift ;;
-m|--mode) mode="single"; shift ;;
# ... (same format for other required arguments)
*) echo "Unknown parameter passed: $1" ;;
esac
shift
done
./bin/sh/tools/build.sh -e local -m $mode -p local
For now, the $mode variable seems to not be set if I don't set it, how can I have a default value for this variable ? (default is single)
What I want is the user to call the script like (reload is true, mode is cluster):
bin/script.sh -r --mode=cluster
Or by default (reload is false, mode is single):
bin/script.sh
Is this the good way to wait for parameters ? I read other ways, but no real explanations.
Thanks.
There is no universal best/cleanest method; it eventually comes down to what works best for you; some ideas:
set mode to a default value before the while/case loop
after the while/case loop test mode and if unset/undefined then set to a
default value (should probably play it safe and unset mode before the while/case loop though in this case you might as well see idea #1)
pass to build.sh wrapped in double quotes (ie, build.sh -e local -m "$mode" -p local) and have build.sh test for the -m argument being unset/undefined and set to a default value
a variation on #2 and #3 is to use parameter substitution when
passing mode to build.sh, eg: build.sh -e local -m "${mode:-default_value}" -p local
if your script sources a config/ini file you could assign mode a
default value in said config/ini file and then make sure said config/ini file is sourced before the while/case loop
If you set the mode beforehand, that will be its default. If you want parameter with assignment, you need to grab $2 and shift twice.
mode="single"
while [[ "$#" -gt 0 ]]; do
case $1 in
-h|--help) helpFunction; shift ;;
-r|--reload) reload=true; shift ;;
-m|--mode) mode="$2"; shift; shift ;;
# ... (same format for other required arguments)
*) echo "Unknown parameter passed: $1" ;;
esac
shift
done
I think it's also good to restore the positional arguments. Since you may want to use them, but that depends on your script.
mode="single"
reload="false"
POSITIONAL=()
while [[ "$#" -gt 0 ]]; do
case $1 in
-h | --help)
helpFunction
shift
;;
-r | --reload)
reload=true
shift
;;
-m | --mode)
mode="$2"
shift
shift
;;
*)
POSITIONAL+=("$1")
shift
;;
esac
done
set -- "${POSITIONAL[#]}"
COMMAND="$1"
Then you could invoke a function that is called like the $COMMAND and pass the rest of the params with $#, and shift again and set the subcommand to $1. You can do this infinitely to get a chain of (sub)commands.
You can still check if a valid command was provided before you try to call the $COMMAND.
COMMAND_LIST="add issue revoke"
case $COMMAND_LIST in
*"$COMMAND"*)
"$COMMAND" "$#"
;;
*)
helpFunction
echo "ERROR: Unknown command: $COMMAND"
exit 1
;;
esac
But that is of course only if your script supports commands/subcommands. Otherwise, it makes sense to error right away like you did, if there is an unknown parameter.

Getopts: how to manager properly optional arguments?

I'm struggling with the following code.
#!/bin/bash
test-one () {
if [[ ! -z $1 ]] ; then
echo "You are in function test-one with arg $1"
else
echo "You are in function test-one with no args"
fi
}
while getopts ":a:b:" opt; do
case $opt in
a) test-one ${OPTARG}
exit;;
b) FOO=${OPTARG}
exit;;
esac
done
I's just like to call the function test-one whether the optional argument is passed or not.
What I am looking for is:
./script.sh -a argument1
would results in:
You are in function test-one with arg argument1
While:
./script.sh -a
would results in:
You are in function test-one with no args
Far by now the example "./script.sh -a" simply skip the function call ...
What am I doing wrong?
Thanks!
Regarding error-reporting, there are two modes getopts can run in:
verbose mode & silent mode
For productive scripts I recommend to use the silent mode, since everything looks more professional, when you don't see annoying standard messages. Also it's easier to handle, since the failure cases are indicated in an easier way.
Verbose Mode
invalid option VARNAME is set to ?(question-mark) and OPTARG is unset
required argument not found VARNAME is set to ?(question-mark), OPTARG is unset and an error message is printed
Silent Mode
invalid option VARNAME is set to ?(question-mark) and OPTARG is set to the (invalid) option character
required argument not found VARNAME is set to :(colon) and OPTARG contains the option-character in question
Try this:
#!/bin/bash
while getopts ":a:" opt; do
case $opt in
a)
echo "-a was triggered, Parameter: $OPTARG" >&2
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:) echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
When a -a is passed you will pass the value on the command line that is given as the argument to -a, stored in OPTARG. In your second example you there is no such argument, so the value gets an empty string, which is dutifully passed to the function.
Unfortunately here, a 0-length string is not preserved as an argument, and at least would in fact be a 0-length string which would "pass" the -z test.
So for "what you are doing wrong" it seems that you are expecting an empty argument to be treated as a non empty argument. You could test OPTARG before using it as an argument if you wanted to verify that -a did in fact have a value passed along with it.

Having getopts in a seperate function in a bash script

I have written the following shell script to parse arguments and print. Its not working as intended. I am sure that I am not passing correct argument to the function thats evaluating the optional params. Can some one please help is correcting it
#! /bin/sh
MAX_NO_OF_DATABASE=500;
MAX_NO_OF_CONCURRENT_REQUEST=500;
MAX_NO_OF_REQUEST=500;
function showUsage(){
echo "Sample Usage : ./benchmarking.sh <CORE_URL> <SYNC_SERVER_URL> [-d MAX_NO_OF_DATABASE] [-c MAX_NO_OF_CONCURRENT_REQUEST] [-n MAX_NO_OF_REQUEST]";
exit;
}
function readArguements(){
# Check for core url
if [ -z $1 ]; then
echo "CORE_URL is not specified"
showUsage
fi
# Check for sync server url
if [ -z $2 ]; then
echo "SYNC_SERVER_URL is not specified"
showUsage
fi
}
function readOptionalArguements(){
# Check for the optional parameters
while getopts dcn: opt
do
case $opt in
d) MAX_NO_OF_DATABASE="$OPTARG";;
c) MAX_NO_OF_CONCURRENT_REQUEST="$OPTARG";;
n) MAX_NO_OF_REQUEST="$OPTARG";;
esac
done
}
readArguements $*
readOptionalArguements $*
echo "$1 $2 $MAX_NO_OF_DATABASE $MAX_NO_OF_CONCURRENT_REQUEST $MAX_NO_OF_REQUEST"
When I run it ./benchmarker.sh core_url sync_url -d 500 -c 100 -n 200
It prints as
core_url sync_url 500 500 500
I had debugging on and I could see that it does not evaluate the switch block. Am I passing the correct arguments to readOptionalArguements
You should set the string to d:c:n:, because all three options take parameters.
Read carefully what man bash says about getopts:
When the end of options is encountered, getopts exits with a return
value greater than zero. OPTIND is set to the index of the first non-
option argument, and name is set to ?.
Therefore, you have to process the first two non-option parameters before processing the options.
core_url=$1
sync_server=$2
shift 2
readArguements "$#"
readOptionalArguements "$#"
Also, if you are using bash, do not write #!/bin/sh in the shebang line.
getopts stops on the first non-option argument, so you need to shift-out those non-option arguments before using them in getopts.
So for example:
function readOptionalArguements(){
# skip two mandatory arguments
shift 2
# Check for the optional parameters
while getopts d:c:n: opt
do
case $opt in
d) MAX_NO_OF_DATABASE="$OPTARG";;
c) MAX_NO_OF_CONCURRENT_REQUEST="$OPTARG";;
n) MAX_NO_OF_REQUEST="$OPTARG";;
esac
done
}
Also, you should declare a local OPTIND to keep that variable safe in case you call the function multiple times.
I believe, the problem may be in the readOptionalArguments method (btw, this is misspelled in your question). The following seems to work for me (in bash)
function readOptionalArguements() {
# Check for the optional parameters
while getopt "d:c:n:" $*
do
echo "opt: <$1>";
case $1 in
-d) shift; MAX_NO_OF_DATABASE="$1"; shift;;
-c) shift; MAX_NO_OF_CONCURRENT_REQUEST="$1"; shift;;
-n) shift; MAX_NO_OF_REQUEST="$1"; shift;;
--) break;;
*) break;;
esac
done
}

Reading $OPTARG for optional flags?

I'd like to be able to accept both mandatory and optional flags in my script. Here's what I have so far.
#!bin/bash
while getopts ":a:b:cdef" opt; do
case $opt in
a ) APPLE="$OPTARG";;
b ) BANANA="$OPTARG";;
c ) CHERRY="$OPTARG";;
d ) DFRUIT="$OPTARG";;
e ) EGGPLANT="$OPTARG";;
f ) FIG="$OPTARG";;
\?) echo "Invalid option: -"$OPTARG"" >&2
exit 1;;
: ) echo "Option -"$OPTARG" requires an argument." >&2
exit 1;;
esac
done
echo "Apple is "$APPLE""
echo "Banana is "$BANANA""
echo "Cherry is "$CHERRY""
echo "Dfruit is "$DFRUIT""
echo "Eggplant is "$EGGPLANT""
echo "Fig is "$FIG""
However, the output for the following:
bash script.sh -a apple -b banana -c cherry -d dfruit -e eggplant -f fig
...outputs this:
Apple is apple
Banana is banana
Cherry is
Dfruit is
Eggplant is
Fig is
As you can see, the optional flags are not pulling the arguments with $OPTARG as it does with the required flags. Is there a way to read $OPTARG on optional flags without getting rid of the neat ":)" error handling?
=======================================
EDIT: I wound up following the advice of Gilbert below. Here's what I did:
#!/bin/bash
if [[ "$1" =~ ^((-{1,2})([Hh]$|[Hh][Ee][Ll][Pp])|)$ ]]; then
print_usage; exit 1
else
while [[ $# -gt 0 ]]; do
opt="$1"
shift;
current_arg="$1"
if [[ "$current_arg" =~ ^-{1,2}.* ]]; then
echo "WARNING: You may have left an argument blank. Double check your command."
fi
case "$opt" in
"-a"|"--apple" ) APPLE="$1"; shift;;
"-b"|"--banana" ) BANANA="$1"; shift;;
"-c"|"--cherry" ) CHERRY="$1"; shift;;
"-d"|"--dfruit" ) DFRUIT="$1"; shift;;
"-e"|"--eggplant" ) EGGPLANT="$1"; shift;;
"-f"|"--fig" ) FIG="$1"; shift;;
* ) echo "ERROR: Invalid option: \""$opt"\"" >&2
exit 1;;
esac
done
fi
if [[ "$APPLE" == "" || "$BANANA" == "" ]]; then
echo "ERROR: Options -a and -b require arguments." >&2
exit 1
fi
Thanks so much, everyone. This works perfectly so far.
: means "takes an argument", not "mandatory argument". That is, an option character not followed by : means a flag-style option (no argument), whereas an option character followed by : means an option with an argument.
Thus, you probably want
getopts "a:b:c:d:e:f:" opt
If you want "mandatory" options (a bit of an oxymoron), you can check after argument parsing that your mandatory option values were all set.
It isn't easy... Any "optional" option arguments must actually be required as far as getopts will know. Of course, an optional argument must be a part of the same argument to the script as the option it goes with. Otherwise an option -f with an optional argument and an option -a with a required argument can get confused:
# Is -a an option or an argument?
./script.sh -f -a foo
# -a is definitely an argument
./script.sh -f-a foo
The only way to do this is to test whether the option and its argument are in the same argument to the script. If so, OPTARG is the argument to the option. Otherwise, OPTIND must be decremented by one. Of course, the option is now required to have an argument, meaning a character will be found when an option is missing an argument. Just use another case to determine if any options are required:
while getopts ":a:b:c:d:e:f:" opt; do
case $opt in
a) APPLE="$OPTARG";;
b) BANANA="$OPTARG";;
c|d|e|f)
if test "$OPTARG" = "$(eval echo '$'$((OPTIND - 1)))"; then
OPTIND=$((OPTIND - 1))
else
case $opt in
c) CHERRY="$OPTARG";;
d) DFRUIT="$OPTARG";;
...
esac
fi ;;
\?) ... ;;
:)
case "$OPTARG" in
c|d|e|f) ;; # Ignore missing arguments
*) echo "option requires an argument -- $OPTARG" >&2 ;;
esac ;;
esac
done
This has worked for me so far.
For bash, this is my favorite way to parse/support cli args. I used getopts and it was too frustrating that it wouldn't support long options. I do like how it works otherwise - especially for built-in functionality.
usage()
{
echo "usage: $0 -OPT1 <opt1_arg> -OPT2"
}
while [ "`echo $1 | cut -c1`" = "-" ]
do
case "$1" in
-OPT1)
OPT1_ARGV=$2
OPT1_BOOL=1
shift 2
;;
-OPT2)
OPT2_BOOL=1
shift 1
;;
*)
usage
exit 1
;;
esac
done
Short, simple. An engineer's best friend!
I think this can be modified to support "--" options as well...
Cheers =)
Most shell getopts have been annoying me for a long time, including lack of support of optional arguments.
But if you are willing to use "--posix" style arguments, visit bash argument case for args in $#
Understanding bash's getopts
The bash manual page (quoting the version 4.1 manual) for getopts says:
getopts optstring name[args]
getopts is used by shell scripts to parse positional parameters. optstring contains
the option characters to be recognized; if a character is followed by a
colon, the option is expected to have an argument, which should be separated
from it by white space. The colon (‘:’) and question mark (‘?’) may not be
used as option characters. Each time it is invoked, getopts places the next
option in the shell variable name, initializing name if it does not exist, and the
index of the next argument to be processed into the variable OPTIND. OPTIND
is initialized to 1 each time the shell or a shell script is invoked. When an
option requires an argument, getopts places that argument into the variable
OPTARG. The shell does not reset OPTIND automatically; it must be manually
reset between multiple calls to getopts within the same shell invocation if a
new set of parameters is to be used.
When the end of options is encountered, getopts exits with a return value
greater than zero. OPTIND is set to the index of the first non-option argument,
and name is set to ‘?’.
getopts normally parses the positional parameters, but if more arguments are
given in args, getopts parses those instead.
getopts can report errors in two ways. If the first character of optstring is a
colon, silent error reporting is used. In normal operation diagnostic messages
are printed when invalid options or missing option arguments are encountered.
If the variable OPTERR is set to 0, no error messages will be displayed, even if
the first character of optstring is not a colon.
If an invalid option is seen, getopts places ‘?’ into name and, if not silent,
prints an error message and unsets OPTARG. If getopts is silent, the option
character found is placed in OPTARG and no diagnostic message is printed.
If a required argument is not found, and getopts is not silent, a question mark
(‘?’) is placed in name, OPTARG is unset, and a diagnostic message is printed. If
getopts is silent, then a colon (‘:’) is placed in name and OPTARG is set to the
option character found.
Note that:
The leading colon in the option string puts getopts into silent mode; it does not generate any error messages.
The description doesn't mention anything about optional option arguments.
I'm assuming that you are after functionality akin to:
script -ffilename
script -f
where the flag f (-f) optionally accepts an argument. This is not supported by bash's getopts command. The POSIX function getopt() barely supports that notation. In effect, only the last option on a command line can have an optional argument under POSIX.
What are the alternatives?
In part, consult Using getopts in bash shell script to get long and short command-line options.
The GNU getopt (singular!) program is a complex beastie that supports long and short options and supports optional arguments for long options (and uses GNU getopt(3). Tracking its source is entertaining; the link on the page at die.net is wrong; you'll find it in a sub-directory under ftp://ftp.kernel.org/pub/linux/utils/util-linux (without the -ng). I've not tracked down a location at http://www.gnu.org/ or http://www.fsf.org/ that contains it.
#!/bin/bash
while getopts ":a:b:c:d:e:f:" opt; do
case $opt in
a ) APPLE="$OPTARG";;
b ) BANANA="$OPTARG";;
c ) CHERRY="$OPTARG";;
d ) DFRUIT="$OPTARG";;
e ) EGGPLANT="$OPTARG";;
f ) FIG="$OPTARG";;
\?) echo "Invalid option: -"$OPTARG"" >&2
exit 1;;
: ) echo "Option -"$OPTARG" requires an argument." >&2
exit 1;;
esac
done
echo "Apple is "$APPLE""
echo "Banana is "$BANANA""
echo "Cherry is "$CHERRY""
echo "Dfruit is "$DFRUIT""
echo "Eggplant is "$EGGPLANT""
echo "Fig is "$FIG""

How to create a flag with getopts to run a command

I need help with my getopts, i want to be able to run this command ( mount command) only if i pass a flag ( -d in this case).
below output is what i have on my script but it doesn't seem to work.
CHECKMOUNT=" "
while getopts ":d" opt
do
case "$opt" in
d) CHECKMOUNT="true" ;;
usage >&2
exit 1;;
esac
done
shift `expr $OPTIND-1`
FS_TO_CHECK="/dev"
if [ "$CHECKMOUNT" = "true" ]
then
if cat /proc/mounts | grep $FS_TO_CHECK > /dev/null; then
# Filesystem is mounted
else
# Filesystem is not mounted
fi
fi
Your script has a number of problems.
Here is the minimal list of fixes to get it working:
While is not a bash control statement, it's while. Case is important.
Whitespace is important: if ["$CHECKMOUNT"= "true"] doesn't work and should cause error messages. You need spaces around the brackets and around the =, like so: if [ "$CHECKMOUNT" = "true" ].
Your usage of getopts is incorrect, I'm guessing that you mistyped this copying an example: While getopts :d: opt should be: while getopts ":d" opt.
Your usage of shift is incorrect. This should cause error messages. Change this to: shift $((OPTIND-1)) if you need to shift OPTIND.
The bare text unknocn flag seems like a comment, precede it with #, otherwise you'll get an error when using an unknown option.
There is no usage function. Define one, or change usage in your \?) case to an echo with usage instructions.
Finally, if your script only requires a single optional argument, you might also simply process it yourself instead of using getopt - the first argument to your script is stored in the special variable $1:
if [ "$1" = "-d" ]; then
CHECKMOUNT="true"
elif [ "$1" != "" ]; then
usage >&2
exit 1
fi

Resources