Bash script command handler to replace long if chain - bash

Let's say I have a bunch of if statements with the form:
if (some flag variable/argument is set) then
execute another command or bash script
This is a bit troublesome to maintain, so I was wondering if there was some other way of doing this. While this guide is for node.js, I was wondering if it is possible to achieve something similar in bash

I wonder if you're looking for something like this:
#!/bin/bash
# initialize global options
debug=false
verbose=0
main() {
local OPTIND
while getopts :hdv: opt; do
case $opt in
h) show_help; exit ;;
d) debug=true ;;
v) verbose=$OPTARG;;
:) echo "error: missing argument for -$OPTARG"; exit 1;;
?) echo "error: unknown option -$OPTARG"; exit 1 ;;
esac
done
shift $((OPTIND-1))
if [[ -z $1 ]]; then
echo "error: missing subcommand"
show_help
exit 1
fi
case $1 in
bar|baz)
# invoke the command with the arguments
"$#" ;;
*) echo "error: unknown subcommand $1"; exit 1;;
esac
}
show_help() {
echo "usage: $(basename "$0") [global opts] subcommand [local opts and args]"
echo "... more details..."
}
bar() {
echo "you called $FUNCNAME with args $*"
}
baz() {
echo "this is baz"
local OPTIND
while getopts :ab opt; do
case $opt in
a|b) echo "you selected option $opt";;
?) echo "unknown option -$OPTARG";;
esac
done
if $debug; then echo "some debug message"; fi
(( verbose > 0 )) && echo "some verbose message"
}
main "$#"

You could write a wrapper function that checks the variable and then executes the command passed in:
#!/bin/bash
run_if_set() {
local var=$1
shift
(($# == 0)) && return # nothing to run
[[ $var ]] && "$#" # execute only if var is set to a non-empty string
}
Then replace your if statements with:
run_if_set "$var" command ...
which is slightly more readable than
if [[ $var ]]; then
command ...
fi
or
[[ $var ]] && command ...

Related

How to write if command with arguments

I've an question, how to use *if *or *case * statements to do below command :
command.sh --value string ?
I tried :
case "$1" in
[--value ])
echo "You choose value"
;;
*)
echo "plz chs diff one"
;;
esac
But it is not what I want to achieve.
I want to run command.sh --value string , where string is the first argument.
getopts is a tool that can be used to parse arguments
usage() {
echo "Usage: $0 [-v]" 1>&2
}
exit_abnormal() {
usage
exit 1
}
while getopts "v" arg; do
case $arg in
v) USE_VALUE='true' ;;
?)
echo "Invalid option: -${OPTARG}."
echo
usage
;;
esac
done
shift
if [[ $USE_VALUE == "true" ]]; then
...
fi
You can try this:
if [ "$1" = "--value" ]; then
echo "You choose value $2"
else
echo "plz chs diff one"
fi

using getopts did not get the input value

I am running the below script, but it looks like the $filename or $srvname did not get the input value.
say for eg: ./test.sh -n abcd.net gives the output echo 'Filename or node name must be defined.'
it means that, the $srvname did not get the value "abcd.net", please advise am i doing anything wrong. ?
set -x
usage () {
echo "usage: $0 -n <nodename>"
echo "usage: $0 -f <filename>"
echo "usage: $0 -h <help>"
}
while getopts ":nfh:" opt; do
case "$opt" in
n) srvname="$OPTARG" ;;
f) filename="$OPTARG" ;;
h) # help
usage
exit 0
;;
:) echo "Error: -$OPTARG requires an argument"
usage
exit 1
;;
?) echo "Error: unknown option -$OPTARG"
usage
exit 1
;;
esac
done
function dosomecheck {
echo "do some checks"
}
if [ "$filename" != "" ] ; then
# read file
for x in `cat $filename` ; do
dosomecheck $x
done
fi
if [ "$srvname" != "" ] ; then
# read file
for x in $srvname ; do
dosomecheck $x
done
fi
Thanks in advance
Try doing:
while getopts ":n:f:h" opt;
because -n and -f takes argument while -h doesn't.

How to pass a long option to a bash script?

./script.sh -abc hello
How can I write my script to use '-abc' as the option and 'hello' as the value to that option?
I should be able to pass this value to all the functions in this script. Lets say I have 2 functions: X and Y.
Use this in your script:
[[ $1 == -abc ]] && value="$2" || echo invalid option
If you don't want to print any messages on wrong option or no option, then omit the || echo ... part, value will be empty.
If you want to make the second argument a must, then:
[[ $1 == -abc ]] && [[ $2 != "" ]] && value="$2" || echo invalid option
Using if else loop will give you complete control over this:
if [[ $1 == -abc ]]; then
#if first option is valid then do something here
if [[ $2 != "" ]]; then
value="$2"
else
#if second option is not given then do something here
echo invalid option
fi
else
echo invalid option
#if first option is invalid then do something here
fi
If you want to make the first argument a must too, then change the first if statement line to
if [[ $1 == -abc && $1 != "" ]]; then
If you want to pass as many arguments as you wish and process them,
then use something like this:
#!/bin/bash
opts=( "$#" )
#if no argument is passed this for loop will be skipped
for ((i=0;i<$#;i++));do
case "${opts[$i]}" in
-abc)
# "${opts[$((i+1))]}" is the immediately follwing option
[[ "${opts[$((i+1))]}" != "" ]] &&
value="${opts[$((i+1))]}"
echo "$value"
((i++))
#skips the nex adjacent argument as it is already taken
;;
-h)
#dummy help option
echo "Options are [-abc value], -h"
;;
*)
#other unknown options
echo invalid option
break
;;
esac
done
This is an example of handling multiple arguments with only two options available -abc value and -h
bash doesn't have a built in command for processing long arguments. In order to parse long options in a shell script, you'll need to iterate over the arguments list yourself.
Here's one approach:
#!/bin/sh
is_option_arg () {
case $1 in
-*)
return 1
;;
*)
return 0
;;
esac
}
usage () {
echo "$(basename "$0") -abc ARG -def ARG -verbose"
}
OPT_ABC=
OPT_DEF=
OPT_VERBOSE=false
while [ "$#" -gt 0 ]; do
case $1 in
-abc)
shift
{ [ "$#" -ne 0 ] && is_option_arg "$1"; } || { usage >&2; exit 1; }
OPT_ABC=$1
;;
-def)
shift
{ [ "$#" -ne 0 ] && is_option_arg "$1"; } || { usage >&2; exit 1; }
OPT_DEF=$1
;;
-verbose)
OPT_VERBOSE=true
;;
*)
break
;;
esac
shift
done
echo "OPT_ABC=$OPT_ABC"
echo "OPT_DEF=$OPT_DEF"
echo "OPT_VERBOSE=$OPT_VERBOSE"
if [ "$#" -gt 0 ]; then
echo "Remaining args:"
for arg in "$#"; do
echo "$arg"
done
fi
You pretty much have to implement it yourself manually. Here's one way:
abc=
while [[ "$1" == -* ]]; do
opt=$1
shift
case "$opt" in
-abc)
if (( ! $# )); then
echo >&2 "$0: option $opt requires an argument."
exit 1
fi
abc="$1"
shift
;;
*)
echo >&2 "$0: unrecognized option $opt."
exit 2
;;
esac
done
echo "abc is '$abc', remaining args: $*"
Some sample runs of the above:
(0)$ ./script.sh
abc is '', remaining args:
(0)$ ./script.sh hello
abc is '', remaining args: hello
(0)$ ./script.sh -abc hello
abc is 'hello', remaining args:
(0)$ ./script.sh -abc hello there
abc is 'hello', remaining args: there
(0)$ ./script.sh -abc
./script.sh: option -abc requires an argument.
(1)$ ./script.sh -bcd
./script.sh: unrecognized option -bcd.
(2)$

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
[...]

How to create a bash script with optional parameters for a flag

I'm trying to create a script which will have a flag with optional options. With getopts it's possible to specify a mandatory argument (using a colon) after the flag, but I want to keep it optional.
It will be something like this:
./install.sh -a 3
or
./install.sh -a3
where 'a' is the flag and '3' is the optional parameter that follows a.
Thanks in advance.
The getopt external program allows options to have a single optional argument by adding a double-colon to the option name.
# Based on a longer example in getopt-parse.bash, included with
# getopt
TEMP=$(getopt -o a:: -- "$#")
eval set -- "$TEMP"
while true ; do
case "$1" in
-a)
case "$2" in
"") echo "Option a, no argument"; shift 2 ;;
*) echo "Option a, argument $2"; shift 2;;
esac ;;
--) shift; break ;;
*) echo "Internal error!"; exit 1 ;;
esac
done
The following is without getopt and it takes an optional argument with the -a flag:
for WORD; do
case $WORD in
-a?) echo "single arg Option"
SEP=${WORD:2:1}
echo $SEP
shift ;;
-a) echo "split arg Option"
if [[ ${2:0:1} != "-" && ${2:0:1} != ""]] ; then
SEP=$2
shift 2
echo "arg present"
echo $SEP
else
echo "optional arg omitted"
fi ;;
-a*) echo "arg Option"
SEP=${WORD:2}
echo $SEP
shift ;;
-*) echo "Unrecognized Short Option"
echo "Unrecognized argument"
;;
esac
done
Other options/flags also can be added easily.
Use the getopt feature. On most systems, man getopt will yield documentation for it, and even examples of using it in a script. From the man page on my system:
The following code fragment shows how one might process the arguments
for a command that can take the options -a and -b, and the option -o,
which requires an argument.
args=`getopt abo: $*`
# you should not use `getopt abo: "$#"` since that would parse
# the arguments differently from what the set command below does.
if [ $? != 0 ]
then
echo 'Usage: ...'
exit 2
fi
set -- $args
# You cannot use the set command with a backquoted getopt directly,
# since the exit code from getopt would be shadowed by those of set,
# which is zero by definition.
for i
do
case "$i"
in
-a|-b)
echo flag $i set; sflags="${i#-}$sflags";
shift;;
-o)
echo oarg is "'"$2"'"; oarg="$2"; shift;
shift;;
--)
shift; break;;
esac
done
echo single-char flags: "'"$sflags"'"
echo oarg is "'"$oarg"'"
This code will accept any of the following as equivalent:
cmd -aoarg file file
cmd -a -o arg file file
cmd -oarg -a file file
cmd -a -oarg -- file file
In bash there is some implicit variable:
$#: contains number of arguments for a called script/function
$0: contains names of script/function
$1: contains first argument
$2: contains second argument
...
$n: contains n-th argument
For example:
#!/bin/ksh
if [ $# -ne 2 ]
then
echo "Wrong number of argument - expected 2 : $#"
else
echo "Argument list:"
echo "\t$0"
echo "\t$1"
echo "\t$2"
fi
My solution:
#!/bin/bash
count=0
skip=0
flag="no flag"
list=($#) #put args in array
for arg in $# ; do #iterate over array
count=$(($count+1)) #update counter
if [ $skip -eq 1 ]; then #check if we have to skip this args
skip=0
continue
fi
opt=${arg:0:2} #get only first 2 chars as option
if [ $opt == "-a" ]; then #check if option equals "-a"
if [ $opt == $arg ] ; then #check if this is only the option or has a flag
if [ ${list[$count]:0:1} != "-" ]; then #check if next arg is an option
skip=1 #skip next arg
flag=${list[$count]} #use next arg as flag
fi
else
flag=${arg:2} #use chars after "-a" as flag
fi
fi
done
echo $flag

Resources