I have a script myscript
that should do the following:
myscript
myscript myarg
myscript -a 1 -b 2 -c 3 ...
myscript myarg -a 1 -b 2 -c 3 ...
I cannot get 4. to work, it seems, that using an argument in front of the options (which I take from getopts
), interferes with the way how getopts
handles everything.
The code I'm using is basically like this:
while getopts "a:b:c:" opt; do
case ${opt} in
a)
var_a=$OPTARG
;;
... more options here
esac
done
if [ ! -z "$1" ] && [[ ! "$1" =~ ^- ]]; then
... do stuff with first argument - if it's there and it's not just an option
fi
If there is a simple solution to this problem, I'd be very thankful!
You can use shift
in the last if
of the script (the general idea is already there, as you write "do stuff with first argument - if it's there and it's not just an option").