Having getopts in a seperate function in a bash script - bash

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
}

Related

How to take 2 argument from getopts

I am creating a bash script that will two argument from the user from command line . But i am not sure how I can take 2 argument from the user and both arguments are required if not passed will show error and return from the script . Below is the code i am using to take argument from the user , but currently my getopts is taking only one argument.
optspec="h-:"
while getopts "$optspec" optchar; do
case "${optchar}" in
-)
case "$OPTARG" in
file)
display_usage ;;
file=*)
INPUTFILE=${OPTARG#*=};;
esac;;
h|*) display_usage;;
esac
done
How could i add an an option to take one more args from command line. Like below
script.sh --file="abc" --date="dd/mm/yyyy"
getopts does not support long arguments. It only supports single letter arguments.
You can use getopt. It is not as widely available as getopts, which is from posix and available everywhere. getopt will be for sure available on any linux and not only. On linux it's part of linux-utils, a group of most basic utilities like mount or swapon.
Typical getopt usage looks like:
if ! args=$(getopt -n "your_script_name" -oh -l file:,date: -- "$#"); then
echo "Error parsing arguments" >&2
exit 1
fi
# getopt parses `"$#"` arguments and generates a nice looking string
# getopt .... -- arg1 --file=file arg2 --date=date arg3
# would output:
# --file file --date date -- arg1 arg2 arg3
# the idea is to re-read bash arguments using `eval set`
eval set -- "$args"
while (($#)); do
case "$1" in
-h) echo "help"; exit; ;;
--file) file="$2"; shift; ;;
--date) date="$2"; shift; ;;
--) shift; break; ;;
*) echo "Internal error - programmer made an error with this while or case" >&2; exit 1; ;;
esac
shift
done
echo file="$file" date="$date"
echo Rest of arguments: "$#"

getopts called in function not picking up flags [duplicate]

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.

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

shell script arguments non positional

Is there a way to feed non positional arguments to a shell script?
Meaning explicitly specify some kind of flag?
. myscript.sh value1 value2
. myscript.sh -val1=value1 -val2=value2
You can use getopts, but I don't like it because it's complicated to use and it doesn't support long option names (not the POSIX version anyway).
I recommend against using environment variables. There's just too much risk of name collision. For example, if your script reacts differently depending on the value of the ARCH environment variable, and it executes another script that (unbeknownst to you) also reacts to the ARCH environment variable, then you probably have a hard-to-find bug that only shows up occasionally.
This is the pattern I use:
#!/bin/sh
usage() {
cat <<EOF
Usage: $0 [options] [--] [file...]
Arguments:
-h, --help
Display this usage message and exit.
-f <val>, --foo <val>, --foo=<val>
Documentation goes here.
-b <val>, --bar <val>, --bar=<val>
Documentation goes here.
--
Treat the remaining arguments as file names. Useful if the first
file name might begin with '-'.
file...
Optional list of file names. If the first file name in the list
begins with '-', it will be treated as an option unless it comes
after the '--' option.
EOF
}
# handy logging and error handling functions
log() { printf '%s\n' "$*"; }
error() { log "ERROR: $*" >&2; }
fatal() { error "$*"; exit 1; }
usage_fatal() { error "$*"; usage >&2; exit 1; }
# parse options
foo="foo default value goes here"
bar="bar default value goes here"
while [ "$#" -gt 0 ]; do
arg=$1
case $1 in
# convert "--opt=the value" to --opt "the value".
# the quotes around the equals sign is to work around a
# bug in emacs' syntax parsing
--*'='*) shift; set -- "${arg%%=*}" "${arg#*=}" "$#"; continue;;
-f|--foo) shift; foo=$1;;
-b|--bar) shift; bar=$1;;
-h|--help) usage; exit 0;;
--) shift; break;;
-*) usage_fatal "unknown option: '$1'";;
*) break;; # reached the list of file names
esac
shift || usage_fatal "option '${arg}' requires a value"
done
# arguments are now the file names
The easiest thing to do is pass them as environment variables:
$ val1=value1 val2=value2 ./myscript.sh
This doesn't work with csh variants, but you can use env if you are using such a shell.
Yes there is. The name is getopts http://www.mkssoftware.com/docs/man1/getopts.1.asp
Example:
#!/bin/bash
while getopts d:x arg
do
case "$arg" in
d) darg="$OPTARG";;
x) xflag=1;;
?) echo >&2 "Usage: $0 [-x] [-d darg] files ..."; exit 1;;
esac
done
shift $(( $OPTIND-1 ))
for file
do
echo =$file=
done
Script has arguments as follows:
- $0 - script name
- $1, $2, $3.... - received arguments
$* = all arguments,
$# = number of arguments
Reference:
http://famulatus.com/ks/os/solaris/item/203-arguments-in-sh-scripts.html

Resources