How do I make the below script to accept the first command as one instead of treating the second character as an argument?
When I execute this script using ./scriptname -i hello
it works as expected but when I use something like ./scriptname -in hello
, it echos n
as the argument.
#!/bin/bash
run(){
local OPTIND opt
while getopts ":i:u:" opt; do
case $opt in
i) echo "Typed -i and $OPTARG";iinput="$OPTARG";;
u)echo "Typed -u";uinput="$OPTARG";;
\?) help;;
esac
done
shift $((OPTIND -1))
echo $iinput
}
run $@
I was expecting the script to default to calling the help function if its called with anything besides i or u. Currently, I get
Typed -i and n n
When I call the script using ./scriptname -in hello