Shell script getopts optarg no value - shell

Could someone examine this code snippet and tell me why when I call this script with a -p abcdef that $OPTARG never has the passed in argument value?
# Process command-line options passed as switches to this script
while getopts "ph:" option; do
case "$option" in
p)
{
if [ -n "$OPTARG" ]; then
echo
echo "##### SCRIPT ERROR: You failed to provide a host prefix. #####"
echo
usage
break
else
echo "Setting host prefix to '$OPTARG'"
echo
HOST_PREFIX=$OPTARG
fi
} ;;
h) usage ;;
'?') usage ;;
*) break ;;
esac
done
shift "$((OPTIND-1))" # Shift off the options and optional --.

All options that requires arguments must be succeeded by :, it should be written as p:h as h option doesn't required arguments.

Related

Is there a way in bash script to have an option to give an argument but it shouldn't a must?

I have a scenario where i would like to assign an option a default value but a user can decide to give it another argument:
Here is an example
check_param() {
for arg in "$#"; do
shift
case "$arg" in
"--force") set -- "$#" "-f" ;;
"--type") set -- "$#" "-t" ;;
"--help") set -- "$#" "-h" ;;
"--"*) echo "Unknown parameter: " $arg; show_help; exit 1 ;;
*) set -- "$#" "$arg"
esac
done
# Standard Variables
force=0
type="daily"
OPTIND=1
while getopts "hft:v" opt
do
case "$opt" in
"f") force=1 ;;
"t") type=${OPTARG} ;;
"h") show_help; exit 0 ;;
"?") show_help; exit 1 ;;
esac
done
shift $(expr $OPTIND - 1) # remove options from positional parameters
From the above example, i would like when the user gives the parameter -t without any argument to apply the default value which is daily , and the user can also use parameter -t with any other argument and that will be checked later in code.
The problem is now the parameter -t must be given an argument due to the colon, but i kinda need for it to do both, with or without argument.
Thanks in advance for any explanations or links to any article that can help.
So according to a suggestion i got Here is the test result
check_param() {
## Standard Variablen der Parameter
force=0
type="daily.0"
## Break down the options in command lines for easy parsing
## -l is to accept the long options too
args=$(getopt -o hft::v -l force,type::,help -- "$#")
eval set -- "$args"
## Debugging mechanism
echo ${args}
echo "Number of parameters $#"
echo "first parameter $1"
echo "Second parameter $2"
echo "third parameter $3"
while (($#)); do
case "$1" in
-f|--force) force=1; ;;
-t|--type) type="${2:-${type}}"; shift; ;;
-h|--help) show_help; exit 0; ;;
--) shift; break; ;;
*) echo "Unbekannter Parameter"; exit 1; ;;
esac
shift
done
echo ${type}
}
check_param $#
echo ${type}
The output:
sh scriptsh -t patch.0
-t '' -- 'patch.0'
Number of parameters 4
first parameter -t
Second parameter
third parameter --
daily.0
daily.0
It still didn't assign the value patch to the variable type
Is there a way in bash script to have an option to give an argument but it shouldn't a must?
Yes, there is a way.
getopts does not supports optional arguments. So... you can:
roll your own bash library for parsing arguments or
use another tool that has support for optional arguments.
A common tool is getopt that should be available on any linux.
args=$(getopt -o hft::v -l force,type::,help -- "$#")
eval set -- "$args"
while (($#)); do
case "$1" in
-f|--force) force=1; ;;
-t|--type) type="${2:-default_value}"; shift; ;;
-h|--help) echo "THis is help"; exit; ;;
--) shift; break; ;;
*) echo "Error parsgin arguments"; exit 1; ;;
esac
shift
done
getopt handles long arguments and reorders arguments, so you can ./prog file1 -t opt and ./prog -t opt file1 with same result.

Shell script with parameters?

I have a shell script that takes parameters, below is the code..
Right now it will only accept parameters if passed if called like this: script.sh --mode=load (or -m=load)
Is there a way to modify this so that it can be called with or without the "=" sign, so that I can call: script.sh --mode load (or -m load)
Ideally needs to work in pure bash as I don't have access to install additional tools, etc.
for i in "$#"
do
case $i in
-m=*|--mode=*)
MODE="${i#*=}"
if [[ $MODE =~ ^(dump|load)$ ]]; then
echo "" > /dev/null
else
bark "Invalid --mode set, set this to dump or load.";
exit 1
fi
;;
-p=*|--db-path=*)
DBPATH="${i#*=}"
;;
-d=*|--dump-dir=*)
DUMPDIR="${i#*=}"
;;
-l=*|--list-file=*)
TABLES="${i#*=}"
# check if file exists on disk
if [ -e $TABLES ]
then
echo "" >> /dev/null
else
bark "Table file not found!";
exit 1
fi
;;
-t=*|--tenant-name=*)
TENANT="${i#*=}"
# check if tenant is correct
if [[ $TENANT =~ ^($TENANT_LIST)$ ]]; then
echo "" >> /dev/null
else
bark "Tenant name does not match, aborting.";
exit 1
fi
;;
-s|--shared)
SHARED=YES
;;
*) usage # unknown option
;;
esac
done
My bash version:
bash --version
GNU bash, version 4.3.22(1)-release (powerpc-ibm-aix5.1.0.0)
Loop on $#. When $1 is "-m", do a shift. So in the next loop $1 will now be the argument to the -m option.
script.sh --mode load
# FIRST LOOP
$# is "--mode load"
$1 is "--mode"
shift
# SECOND LOOP
$# is "load"
$1 is "load"
This is also useful if you can specify many arguments instead of just one like you have right now. Error checking should be done to validate your argument values, and if a user did script.sh --mode with no other argument.
Don't reinvent the wheel.
If you're OK with just 1 character options, use the bash builtin getopts
#!/bin/bash
while getopts :m:p:d:l:t:s opt; do
case $opt in
m) mode=$OPTARG ;;
p) dbpath=$OPTARG ;;
d) dumpdir=$OPTARG ;;
l) tables=$OPTARG
# test file existence
;;
t) tenant=$OPTARG
# test tenant
;;
s) shared=YES ;;
:) echo "Missing argument for option -$OPTARG" >&2
exit 2
;;
*) echo "Invalid option -$OPTARG" >&2
exit 2
;;
esac
done
shift $((OPTIND - 1))
cat << SHOW_VARS
I have:
mode=$mode
dbpath=$dbpath
dumpdir=$dumpdir
tables=$tables
tenant=$tenant
shared=$shared
rest of args=$*
SHOW_VARS
Otherwise, you may be able to use the external getopt program to help parse your args. I don't have an AIX box to test on, so YMMV
tempargs=$(
getopt \
-o m:d:l:t:s \
--long mode:,db-path:,dump-dir:,list-file:,tenant-name:,shared \
-- "$#"
)
if [[ $? -ne 0 ]]; then echo "Error..." >&2; exit 2; fi
eval set -- "$tempargs"
while true; do
case $1 in
-m|--mode) mode=$2; shift 2;;
-p|--db-path) dbpath=$2; shift 2;;
-d|--dump-dir) dumpdir=$2; shift 2;;
-l|--list-file) tables=$2
# test file existence
shift 2
;;
-t|--tenant-name) tenant=$2
# test tenant
shift 2
;;
-s|--shared) shared=YES; shift;;
--) shift; break ;;
*) echo "Error..." >&2; exit 2 ;;
esac
done

getopts when first option is a path in bash

I'm having an issue with getopts in a bash script. Basically my script would have to be called with something like:
./myScript /path/to/a/folder -a -b
What I have at the top of my code is this:
while getopts ":ab" opt; do
case $opt in
a)
variable=a
;;
b)
variable=b
;;
\?)
echo "invalid option -$OPTARG"
exit 0
esac
done
echo "$variable was chosen"
Now, this works as long as I call my script without /path/to/a/folder… How can I make it to work with it instead?
Thanks a lot
If you MUST put a path before the arguments, use a shift command to pop the first positional argument off, and leave the rest for getopts.
# Call as ./myScript /path/to/a/folder -a -b
path_argument="$1"
shift # Shifts away one argument by default
while getopts ":ab" opt; do
case $opt in
a)
variable=a
;;
b)
variable=b
;;
\?)
echo "invalid option -$OPTARG"
exit 0
esac
done
echo "$variable was chosen, path argument was $path_argument"
The more-standard answer, as Etan mentioned, is to put the non-option arguments AFTER the options. Prefer this style, as it makes your script more consistent with built-in POSIX option parsing.
# Call as ./myScript -a -b /path/to/a/folder
while getopts ":ab" opt; do
case $opt in
a)
variable=a
;;
b)
variable=b
;;
\?)
echo "invalid option -$OPTARG"
exit 0
esac
done
shift $((OPTIND - 1)) # shifts away every option argument,
# leaving your path as $1, and every
# positional argument as $#
path_argument="$1"
echo "$variable was chosen, path argument was $path_argument"

Using getopts in bash to get optional input argument [duplicate]

This question already has answers here:
Optional option argument with getopts
(15 answers)
Closed 8 years ago.
I am using getopts to process the input arguments. I have problem in reading optional argument value.
When I invoke the script with arguments test.sh -t test -r server -p password -v 1
$OPTARG is not returning the value of the optional argument -v.
Can anyone let me know how to process the optional argument value?
#!/bin/bash
usage()
{
cat << EOF
usage: $0 options
OPTIONS:
-h Show this message
-t Test type
-r Server address
-p Server root password
-v Verbose
EOF
}
TEST=
SERVER=
PASSWD=
VERBOSE=
echo "======111======"
while getopts "ht:r:p:v" OPTION
do
case $OPTION in
h)
usage
echo "===Option h selected=="
exit 1
;;
t)
TEST=$OPTARG
echo "====option t selected===$TEST"
;;
r)
SERVER=$OPTARG
echo "=====option r selected==="
;;
p)
PASSWD=$OPTARG
echo "====option p selected==="
;;
v)
VERBOSE=$OPTARG
echo "======option v selected===$VERBOSE"
;;
?)
echo "====unknown option selected===="
usage
exit
;;
esac
done
echo "========222===="
Do the thing in the case statement.
case $OPTION in
v)
VERBOSE=$OPTARG
do_the_thing $OPTARG
;;
esac
Do the thing after the case statement.
if [ ! -z "$VERBOSE" ]; then
do_the_thing "$VERBOSE"
else
do_not_do_the_thing
fi

Avoid options to be taken as argument automatically

I'd like to do some error checking for a bash script I am writing. In particular, I wish to ensure the following option not to be considered as the argument of an option (intentionally) left empty.
Let's say the following snippet
while getopts “hhelpc1:2:” OPTION
do
case "$OPTION" in
h|help)
usage
exit 1
;;
1)
var1=${OPTARG}
;;
2)
var2=${OPTARG}
;;
c)
test1
;;
esac
done
Assuming my script is called test.sh
By doing something like
./test.sh -1 -2 dddd -c
In the above circumstance test1 output an error message that -2 option is empty. On the opposite, I'd like to raise a warning for -1 being empty, whereas at present -2 will be taken as the argument for -1.
Any help?
Thanks
Andrea
getopts:
only handles short option names, so you cannot put "help" in your option string -- that means you're looking for "-h", "-e", "-l", "-p"
cannot look for missing arguments the way you're hoping. You'll have to examine $OPTARG to check if it looks like one of your options.
Add a leading : to the opt string to handle getopts errors yourself.
Here's a reworking of your code and I'm sure there are plenty of cases I'm not catching
#!/bin/bash
usage () { echo usage ...; }
test1 () { echo test1; }
shopt -s extglob
while getopts ":hc1:2:" opt
do
case $opt in
h)
usage
exit 1
;;
1)
case $OPTARG in
-[hc2]*)
echo "error: required argument missing for -1"
usage
exit 1
;;
*) var1=$OPTARG
;;
esac
;;
2)
case $OPTARG in
-[hc1]*)
echo "error: required argument missing for -2"
usage
exit 1
;;
*) var2=$OPTARG
;;
esac
;;
c)
test1
;;
:)
echo "error: required argument missing for -$OPTARG"
usage
exit 1
;;
\?)
# unknown argument, handle accordingly
;;
esac
done
shift $((OPTIND - 1))
echo "var1=$var1"
echo "var2=$var2"
echo "rest=$*"
Update 2014-01-23
Here's one technique:
do_test1=false
while getopts ...
case $opt in
...
c) do_test1=true ;;
...
esac
done
shift $((OPTIND - 1))
# execute function "test1" if "-c" was given:
$do_test1 && test1

Resources