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

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

Related

How to extract a particular pattern passed in command line argument in shell script

Like i am passing this argument to command line
trace.sh -f abc -t 20
i want to extrace (-t 20) in a variable. how to do that?
Thanks
Very basic example (not fully error prone)
err() { echo "$#" >&2; return 1; }
declare -A options
options=([f]="abc" [t]="20") #the defaults
while getopts ":f:t:" opt
do
case "$opt" in
f) options[$opt]="$OPTARG" ;;
t) options[$opt]="$OPTARG" ;;
\?) err "Invalid option -$OPTARG" || exit 1 ;;
esac
done
echo "t: ${options[t]}"
echo "f: ${options[f]}"

How to make an argument optional in getopts bash?

I would like to make one of the optional characters (-t) which should not accept any argument in getopts bash. This is where i got so far
while getopts ":hb:q:o:v:t" opt; do
case $opt in
b)
Blasting_list=$OPTARG
;;
l)
query_lincRNA=$OPTARG
;;
q)
query_species=$OPTARG
;;
o)
output=$OPTARG # Output file
;;
t)
species_tree=$OPTARG
;;
h)
usage
exit 1
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
I want to run the above script like this..
bash test.sh -b Blasting_list.txt -l Sample_query.fasta -q Atha -o test_out -v 1e-20 -t
Then it should execute the below loop
(-----)
if [ ! -z $species_tree ];
then
mkdir -p ../RAxML_families
perl /Batch_RAxML.pl aligned_list.txt
rm aligned_list.txt
else
rm aligned_list.txt
fi
(-----)
And if i run like this, it should skip the loop.
bash test.sh -b Blasting_list.txt -l Sample_query.fasta -q Atha -o test_out -v 1e-20
(-----)
(-----)
I tried to play with getopts options but i cannot make it work.
probably the easiest way is to set species_tree to true iff there's the -t command line flag:
species_tree=false # <-- addition here
while getopts ":hb:q:o:v:t" opt; do
case $opt in
...
t)
species_tree=true # <-- change here
;;
...
esac
done
if $species_tree; then # <-- change here
...

How to read inline parameters using command line args in bash script?

I have a bash script which takes few command line args and a filename as inline parameter. I am not able to read the inline parameter.
sh test.sh -a a -b b -c c < pwd.txt
test.sh has
if [ $# = 0 ]
then
echo $USAGE >&2
exit $STATUS_ERROR_FAIL
fi
# Parse command line options.
while getopts a:b:c: OPT;
do
case "$OPT" in
a)
a="$OPTARG"
;;
b)
b="$OPTARG"
;;
c)
c="$OPTARG"
;;
\?)
# getopts issues an error message
echo $USAGE
exit $STATUS_ERROR_FAIL
;;
esac
done
shift $((OPTIND-1))
echo "1=$1"
your script is working fine, your error is
echo "1=$1"
if you want to see your parameter you should add an echo/print in your case
#!/bin/bash
if [ $# = 0 ]
then
echo $USAGE >&2
exit $STATUS_ERROR_FAIL
fi
# Parse command line options.
while getopts a:b:c: OPT;
do
case "$OPT" in
a)
a="${OPTARG}"
echo "a[$a]"
;;
b)
b="${OPTARG}"
echo "b[$b]"
;;
c)
c="${OPTARG}"
echo "c[$c]"
;;
\?)
# getopts issues an error message
echo $USAGE
exit $STATUS_ERROR_FAIL
;;
esac
done
shift $((OPTIND-1))
or
you can add the echo/print at the end of the script.. it's depend by your needs
output
[shell] ➤ ./t -a 1 -b 2 -c 3
a[1]
b[2]
c[3]
Regards
Claudio

Shell script getopts optarg no value

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.

How do you use getopts?

what is the easiest, most straight forward, way to use getopts in bash script.
if i have a script called: myscript and it CAN take the the arguments: -p -r -s -x
if argument x then exit
if argument p then echo "port 10"
if argument s then add 2+2
if argument r then echo env
This is a hypothetical script but I would just like to see an example of how this would be done.
while getopts :xpsr opt; do
case $opt in
x ) exit ;;
p ) echo port 10 ;;
s ) (( 2 + 2 )) ;;
r ) echo env ;;
\? ) echo "${0##*/}" [ -xpsr ]; exit 1 ;;
esac
done
usage()
{
echo "Usage: $0 [-o <offset>] [-h];"
exit 0;
}
# -o (offset) need a value
# -h prints help
offset=0 # 0 is default offset
while getopts o:s opt
do
case "$opt" in
d) offset="$OPTARG";; # changing offset
s) usage # calls function "usage"
\?) echo "$OPTARG is an unknown option"
exit 1;; # all other options
esac
done
shift $((OPTIND-1))

Resources