How to write getopt in bash script [duplicate] - bash

This question already has an answer here:
How to support both short and long options at the same time in bash? [duplicate]
(1 answer)
Closed 7 years ago.
I have a function with a few parameters. For example:
makeUser{
login
email
password}
I want to make flags like -l|--login, -e|--email and -p|--password but I don't know how.
The sample for it should look like the code below:
./script.sh --mode makeUser --login test --email test#test.com -p testxx
How can I achieve that result? I know only how to make it with getopts (short flags).
Should it look like code below?
while true ; do
case "$1" in
-m|--mode)
makeUser)
case "$2" in
-l|--login)
makeUser "$OPTARG"
case "$3" in
-e|--email)
makeUser "$OPTARG"
case "$4" in
-p|--password)
makeUser "$OPTARG"
exit $?
esac ;;
exit $?
esac ;;
exit $?
esac ;;
makeProject)...
makeSite)...
exit $?
esac ;;
done

Using while and shift makes a clean solution for getopt-alike behaviour in bash:
while [ $# -gt 0 ]; do
case "$1" in
-h|"-?"|--help)
shift
echo "usage: $0 [-v] [--mode MODE] [-l login] [-e email] [...]"
exit 0
;;
--mode)
MODE=$2
shift; shift;
;;
-l|--login)
LOGIN=$2
shift; shift;
;;
-e|--email)
EMAIL=$2
shift; shift;
;;
-v|--verbose)
VERBOSE=1
shift;
;;
*)
echo "Error: unknown option '$1'"
exit 1
esac
done
# example function makeUser
makeUser()
{
login=$1
email=$2
echo "function makeUser with login=${login} and email=${email}"
}
if [ "$MODE" == "makeUser" ]; then
makeUser $LOGIN $EMAIL # ... and so on
fi

Related

How to make it manditory for options to be spaced for bash scripts

I have the following script.
I would like to modify it so that if I were to call temp.sh with both the options, I would have to space them. Ie: A call to the script like temp.sh -fc30 should be invalid, rather it should be temp.sh -f -c 30
ARGS=$(getopt -o c:f -l "charlie:fox" -n "temp.sh" -- "$#");
#bad args
if [ $? -ne 0 ];
then
exit 1
fi
eval set --"$ARGS";
while true; do
case "$1" in
-c|--charlie)
shift;
if [ -n "$1" ]; then
echo "-c =: $1";
shift;
fi
;;
-f|--fox)
shift;
echo "fox used";
;;
--)
shift;
break;
;;
esac
done
Just don't use getopt.
#!/bin/bash
# parse options
while [[ $# -gt 0 ]]; do
case $1 in
-c|--charlie)
echo "$1 = $2"
shift
;;
-f|--fox)
echo "fox used"
;;
--)
shift
break
esac
shift
done
# do script

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

How can I accept long arguments using getopts in Bash?

I'm trying to have my getops function run with multiple flags and arguments but instead of short (-f style) flag, I want to accept a long one (--flag style). For example:
if [ $# -lt 1 ]; then
usage >&2
exit 1
else
while $1 "hf:" opt; do
case $opt in
h)
echo "Here is the help menu:"
usage
;;
f)
ls -l $OPTARG >&2
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
:)
echo "Option -$OPTARG requires an argument" >&2
exit 1
;;
esac
done
fi
I would like the -h and -f to be --help and --file respectively.
How do I do that?
getopt will do this for you. It handles short options, long options, options with and without arguments, -- to end option parsing, and more.
Boilerplate usage looks like:
options=$(getopt -o hf: -l help,file: -n "$0" -- "$#") || exit
eval set -- "$options"
while [[ $1 != -- ]]; do
case $1 in
-h|--help) echo "help!"; shift 1;;
-f|--file) echo "file! $2"; shift 2;;
*) echo "bad option: $1" >&2; exit 1;;
esac
done
shift
# Process non-option arguments.
for arg; do
echo "arg! $arg"
done

How to require command line parameters in bash?

I have a bash script where I need to have some parameters. Usage should be only between:
./script.sh --scan [scan type] [keyword]
or
./script.sh --help
In example it should be something like this:
$ ./script.sh
[Usage]
$ ./script.sh --scan
Specify scan type
$ ./script.sh --help --scan
[Usage]
$ ./script.sh --scan short
Specify keyword to search
$ ./script.sh --scan short keyword
[Starts short scanning for "keyword" - go to function where my script is, blah, blah]
$ ./script.sh keyword --scan short
[As above]
$ ./script.sh keyword
[Usage]
How I can achieve this?
This is just off the top of my head:
if [ "$1" = "--scan" ] ; then
if [ "$#" -ge 2 ] ; then
_SCANTYPE=$2
if [ "$#" -ge 3 ] ; then
_KEYWORD=$3
else
echo -n "Specify keyword to search: "
read _KEYWORD
fi
else
echo -n "Specify scan type: "
read _SCANTYPE
echo -n "Specify keyword to search: "
read _KEYWORD
fi
# Actual scan code
else
# Display usage info
fi
This is pretty basic, of course, but hopefully it will get you started.
Almost any Configure script to build a piece of software from source will have what you want. From perl's:
: option parsing
while test $# -gt 0; do
case "$1" in
-d) shift; fastread=yes;;
-e) shift; alldone=cont;;
-f)
shift
cd ..
if test -r "$1"; then
config_sh="$1"
else
echo "$me: cannot read config file $1." >&2
error=true
fi
cd UU
shift;;
--help|\
-h) shift; error=true;;
-r) shift; reuseval=true;;
-s) shift; silent=true; realsilent=true;;
-E) shift; alldone=exit;;
-K) shift; knowitall=true;;
-O) shift; override=true;;
-S) shift; silent=true; extractsh=true;;
-D)
shift
case "$1" in
*=)
echo "$me: use '-U symbol=', not '-D symbol='." >&2
echo "$me: ignoring -D $1" >&2
;;
*=*) echo "$1" | \
sed -e "s/'/'\"'\"'/g" -e "s/=\(.*\)/='\1'/" >> optdef.sh;;
*) echo "$1='define'" >> optdef.sh;;
esac
shift
;;
-U)
shift
case "$1" in
*=) echo "$1" >> optdef.sh;;
*=*)
echo "$me: use '-D symbol=val', not '-U symbol=val'." >&2
echo "$me: ignoring -U $1" >&2
;;
*) echo "$1='undef'" >> optdef.sh;;
esac
shift
;;
-A)
shift
xxx=''
yyy="$1"
zzz=''
uuu=undef
case "$yyy" in
*=*) zzz=`echo "$yyy"|sed 's!=.*!!'`
case "$zzz" in
*:*) zzz='' ;;
*) xxx=append
zzz=" "`echo "$yyy"|sed 's!^[^=]*=!!'`
yyy=`echo "$yyy"|sed 's!=.*!!'` ;;
esac
;;
esac
case "$xxx" in
'') case "$yyy" in
*:*) xxx=`echo "$yyy"|sed 's!:.*!!'`
yyy=`echo "$yyy"|sed 's!^[^:]*:!!'`
zzz=`echo "$yyy"|sed 's!^[^=]*=!!'`
yyy=`echo "$yyy"|sed 's!=.*!!'` ;;
*) xxx=`echo "$yyy"|sed 's!:.*!!'`
yyy=`echo "$yyy"|sed 's!^[^:]*:!!'` ;;
esac
;;
esac
case "$xxx" in
append)
echo "$yyy=\"\${$yyy}$zzz\"" >> posthint.sh ;;
clear)
echo "$yyy=''" >> posthint.sh ;;
define)
case "$zzz" in
'') zzz=define ;;
esac
echo "$yyy='$zzz'" >> posthint.sh ;;
eval)
echo "eval \"$yyy=$zzz\"" >> posthint.sh ;;
prepend)
echo "$yyy=\"$zzz\${$yyy}\"" >> posthint.sh ;;
undef)
case "$zzz" in
'') zzz="$uuu" ;;
esac
echo "$yyy=$zzz" >> posthint.sh ;;
*) echo "$me: unknown -A command '$xxx', ignoring -A $1" >&2 ;;
esac
shift
;;
-V) echo "$me generated by metaconfig 3.5 PL0." >&2
exit 0;;
--) break;;
-*) echo "$me: unknown option $1" >&2; shift; error=true;;
*) break;;
esac
done
For your two examples
./script.sh --scan [scan type] [keyword]
or
./script.sh --help
Try this
#!/bin/bash
usageMsg="usage:ScriptName --scan [scan type] [keyword] OR --help"
case $# in
[123] ) : nothing_may_be_OK ;;
0 ) # no args, display usageMsg
echo "$usageMsg" >&2 ; exit 1 ;;
esac
case "$1" in
--[Hh][Ee][Ll][Pp] ) echo "$usageMsg" >&2 ; exit 1 ;;
--[Ss][Cc][Aa][Nn] )
shift
case $# in
0) # using defaults
scanType="PrimaryScan" # change to your default scanType
keyword="PrimaryKeyWord" # again, change to your default value
;;
1)
echo "$usageMsg" >&2
echo "must provide both [scan type] and [keyword] arguments. Cant continue" >&2
exit 1
;;
2)
scanType="$1"
keyword="$2"
;;
esac # case $#
;;
esac # case $1
#restof your code goes here
echo "running scanner with scanType=$scanType and keyWord=$keyword"
echo ". . . . ."
exit 0
I'm guessing that your usage pattern means --scan OR advanced scan (with details), i.e. --scan [scan type] [keyword]
If you really intend for user to optionally add [scan type] AND/OR [keyword] then you'll need some further use of the techniques in the code above.
IHTH
I finally managed that! The basic version is:
#!/bin/bash
function usage
{
echo "Usage: usage"
exit
}
while [ $# -gt 0 ]; do
case $1 in
-h|--help)
usage
;;
-s|--scan)
scantype="$2"
shift
;;
-*)
usage
;;
*)
keyword="$1"
;;
esac
shift
done
if [ -z "$scantype" ]; then
echo "no scantype"
exit
fi
if [[ "$scantype" != "full" && "$scantype" != "long" && "$scantype" != "extended" && "$scantype" != "short" ]]; then
echo "invalid scantype"
exit
fi
if [ -z "$keyword" ]; then
echo "nokeyword"
exit
fi
echo "scantype: $scantype"
echo "keyword: $keyword"
Anyway thanks for your help :)

Script with non-option and option arguments

I'm trying to handle both optional and mandatory parameter to my bash script. I have following script:
while getopts "a:x:" opt; do
case $opt in
a) echo "option a set: $OPTARG" ;;
x) echo "option x set: $OPTARG" ;;
\?) echo "Invalid option: -$OPTARG" >&2; exit 1;;
esac
done
shift $((OPTIND-1))
echo "mandatory argument $1"
echo "mandatory argument2 $2"
Everything looks ok when I run my script using following command:
./script.sh -a optionA -x optionX mandatory1 mandatory2
But when I mix this params:
./script.sh mandatory1 mandatory2 -a optionA -x optionX
It doesn't... How to make it works for all combination of parameters?
You can iterate between both kinds of argument, I think.
I think this does what you want, and allows you to use -- to prevent the following arguments being interpreted as options.
mandatory=()
while [ $# -gt 0 ] && [ "$1" != "--" ]; do
while getopts "a:x:" opt; do
case $opt in
a) echo "option a set: $OPTARG" ;;
x) echo "option x set: $OPTARG" ;;
\?) echo "Invalid option: -$OPTARG" >&2; exit 1;;
esac
done
shift $((OPTIND-1))
while [ $# -gt 0 ] && ! [[ "$1" =~ ^- ]]; do
mandatory=("${mandatory[#]}" "$1")
shift
done
done
if [ "$1" == "--" ]; then
shift
mandatory=("${mandatory[#]}" "$#")
fi
echo "mandatory argument ${mandatory[0]}"
echo "mandatory argument2 ${mandatory[1]}"
Basically, the idea is to consume all the options with getopt, then consume all the non-options manually, then look for more options with getopt again.
To make it work I had to unset OPTIND after the shift $((OPTIND-1)):
[...]
shift $((OPTIND-1))
unset OPTIND
[...]

Resources