switches for shell script - linux

what is the best way to implement switches [ex: -m] for shell scripts?
I can do it via the switch case statement. But i am curious to know is there any other standard way to get all the arguments into a variable via a switch.
Ex:
-m A1 A2 -c c1 c2
So that,
M[] can take -m
and C[] can all take -c

The best known way is to use getopts, see http://wiki.bash-hackers.org/howto/getopts_tutorial
An example :
#!/bin/bash
while getopts ":a" opt; do
case $opt in
a)
echo "-a was triggered!" >&2
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
esac
done

Related

shell getopts parameters collection issue

I have below code in my shell script.
show_help()
{
cat <<EOF
Usage: ${0##*/} [-h help] [-g GATEWAY_HOSTID] [-t TIMEZONE]
-h display this help and exit
-g GATEWAY_HOSTID zabbix gateway identifier (e.g. '20225')
-t Time Zone TimeZone against which you want to test
EOF
}
OPTIND=1
while getopts "g:h:t" opt; do
case "$opt" in
h)
show_help
exit 0
;;
g)
gateway_hostid=$OPTARG
;;
t)
timezone=$OPTARG
;;
esac
done
shift $((OPTIND-1))
if [[ ! $timezone ]]; then
timezone="UTC"
fi
if [[ ! $gateway_hostid ]]; then
echo "hostid is missing!!! Exiting now."
exit
fi
When I execute script it only takes parameter gateway_hostid and ignores timezone parameter. I am not sure what I am doing wrong here. Also it doesn't show help function as well. Can someone help. below is the syntax for calling script.
./script_name.sh -g 20225 -t Europe/Zurich
./script_name.sh -g 20225 -t CEST
Your problem is with the optstring. You're specifying h: which means that -h requires an option. You are also specifying t without a : meaning t does not expect an option.
The optstring to have g and t take options and h not need one is hg:t:

Combining multiple options into one single option (Getopts)

Due to my lack of thorough understanding using getopts, the title is definitely vague :0. I am currently writing a bash script and I would like to add an option that outputs the other options within the case statement in getopts. For the sake of scaling, I have shortened the program.
#!/bin/bash
while getopts :abc opt
do
case $opt in
a)
echo "Hello"
;;
b)
echo "Goodbye"
c)
:ab #****I WANT -c TO OUTPUT THE RESULTS OF a and b************
;;
esac
done
As you can see in option c, I would like this particular option (-c) to put out both the results of -a and -b. Is there a way to go about this by simply making c call on option a and b?
you can introduce functions to reduce duplications, something like this:
#!/bin/bash
do_a() {
echo "Hello"
}
do_b() {
echo "Goodbye"
}
while getopts :abc opt
do
case $opt in
a)
do_a
;;
b)
do_b
;;
c)
do_a
do_b
;;
esac
done
If you are using a recent version of Bash, instead of terminating case clauses with ;; you could use bash specific ;;& with multiple patterns:
#!/bin/bash
while getopts :abc opt
do
case $opt in
a|c)
echo "Hello"
;;&
b|c)
echo "Goodbye"
;;&
esac
done
And:
$ bash script.bash -a
Hello
$ bash script.bash -c
Hello
Goodbye
Using ‘;;&’ in place of ‘;;’ causes the shell to test the patterns in the next clause, if any, and execute any associated command-list on a successful match.

How get next to next argument in linux shell script?

I have wrote simple shell script test.sh as follows:
while getopts ":A:" OPTION
do
case $OPTION in
A)
echo $OPTARG
?)
echo "no option"
esac
done
And executed the scripts as follows
$ ./test.sh -A 1 2
Now if got argument 1 by $OPTARG but how can i access the second argument ( 2 in this case)?
Regards
Jayesh
There are several options.
(1) You can use shift and take $1
while -n "$1"
do
# do something with $1
shift
done
(2) You can iterate through the args:
for i
do
# do something with $i
done
There are other alternatives also.

GETOPTS passing option to create find function

Im trying desperately finding the solution for my Getopts
For example
#!/bin/bash
while getopts ":a:b:" opt; do
case $opt in
a) find / $OPTARG >&2 ;;
b) 2>/dev/null >&2 ;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
esac
and then as output example
./somefile.sh -a *txt -b
or
./somefile.sh -b -a *txt
but then i want to make sure i can upgrade it further, for examples find only sh files, or
something else.
Its not easy to find, but i hope someone could help me.

issue in getopt , Unix shell script

Hi can someone fix this issue, i am not able to get outpt.
I am not able to get output of -p.
#!/bin/bash
args=`getopt c:m:p $*`
if [ $? != 0 -o $# == 0 ]
then
echo 'Usage: -c <current-dir> -m <my dir> -p <argument>'
exit 1
fi
set -- $args
for i
do
case "$i" in
-c) shift;CURRDIR=$1;shift;shift ;;
-m) MYDIR=$1;shift;;
-p) ARGVAL=$OPTARG;;
esac
done
echo "CURRDIR = $CURRDIR"
echo "MYDIR = $MYDIR"
echo "ARGVAL = $ARGVAL"
./1.sh -c "def" -m "ref" -p "ref -k ref"
Expected output
output -c = "def"
-m ="ref"
-p ="ref -k ref"
getopt
args=`getopt c:m:p $*`
You need to add a colon after the p to indicate that -p takes an argument. Also you should change $* to "$#" for better handling of spaces.
args=`getopt c:m:p: "$#"`
You are also mixing up getopt and getopts. $OPTARG is a getopts feature. With plain getopt and set you should simply use $2 and then shift off the argument.
-p) ARGVAL=$2; shift 2;;
At this point you've done as good as you can with getopt. Unfortunately it doesn't handle the multi-word argument to -p no matter what you do. For that, we need to use getopts.
getopts
From getopt and getopts:
Easier to use and generally better than getopt, though of course not available in csh-like shells. You shouldn't be using those anyway.
This works rather differently than "getopt". First, because it's a built-in, you usually won't find a separate man page for it, though "help getopts" may give you what you need.
The old "getopt" is called once, and it modifies the environment as we saw above. The builtin "getopts" is called each time you want to process an argument, and it doesn't change the original arguments .
Using getopts is a lot simpler. Your entire loop can be simplified to this:
while getopts c:m:p: flag
do
case "$flag" in
c) CURRDIR=$OPTARG;;
m) MYDIR=$OPTARG;;
p) ARGVAL=$OPTARG;;
esac
done
No shifting needed, you just read $OPTARG each time to get each option's value.

Resources