PDA

View Full Version : [Solved] help using getopts


hunty
November 6th, 2009, 05:19
Hi,

first up i use mac os x so please dont hold that against me :)

I have a script that i would like to change so that it behaves a bit better

currently the script is as below

#!/bin/bash

if [ "$1" == "M" ]
then
echo "The Flag M was given"
else
if [ "$1" == "K" ]
then
echo "The Flag K was given"
fi
fi


I call the script by ./script.sh M or ./script K

i would like to call it by ./script -m or ./script -k

and depending upon which flag i give the script i would like it to run my different commands or routines as shown above.
Is there away to do this? I looked at getopts but it confused the hell out of me as all the examples i found were way more complicated than what i needed.

Thanks for any help!

Cheers,

Calum

DutchDaemon
November 6th, 2009, 05:36
Are you planning on using single options only? Then you might as well use 'case'.


case $1 in
-k)
echo "-k detected"
;;
-m)
echo "-m detected"
;;
*)
echo "That's not funny."
;;
esac

hunty
November 6th, 2009, 05:57
thats perfect!

thanks heaps mate!!

cheers,

Calum