Hi there,
I have the following scriptlet:
Code:
while [[ "${1:0:1}" == "-" ]]
do
case ${1} in
"-c")
shift
echo "Matched -c"
dbname=${1}
;;
"-u")
shift
echo "Matched -u"
dbuser=${1}
;;
"-p")
shift
echo "Matched -p"
dbpass=${1}
;;
*)
break
;;
esac
done
if [[ ! -z "${dbname}" ]]
then
echo "Connecting to db ${dbname} with user ${dbuser} and password ${dbpass}"
fi
echo ${*}
This is some work in progress; hence the simple echo instead of really connecting. The idea is quite simple: if this script is called with the parameters -c, -p and -u these values must NOT be echoed by "echo ${*} and the variables have to be set according to the parameters given.
so I need to be able to run my script like this:
Code:
script.sh -c db1 -u dbuser -p dbpass run this.sql
and
Code:
script.sh echo oh my goodness
Somehow, my thinking went wrong and the while loop only takes the first parameter, the shifting done within the case statement (if applicable) does not seem to affect the while loop.
Unfortunately I have bash version 3.2.51, and no means to upgrade it to bash 4.
Getopts would not help me alot here I reckon, because I need to strip stuff from my parameter list; I had hoped shift would let me.