I am using getopts in my script and I want to work it for all the following order of option parsing.
./myscript -c server
./myscript -c -h server
./myscript server -c
./myscript -h server -c
./myscript server
I am using myscript as follows.
#!/bin/bash
while getopts c:h: var
do
case $var in
h) host=$OPTARG;;
c) FLAG=1
esac
done
Here "server" is a argument and should load even -h option specifies or not and also -c option I am using for a FLAG.Is there a way to get this achieved.
Sometimes it's better not to use
getopts
at all:By the way your script would give you a syntax error since you missed the two semicolons:
I fixed my issue by applying some extra validations..
In all the cases I am getting my host in my script as shown in this output.