help using getopts

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

Code:
#!/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
 
Are you planning on using single options only? Then you might as well use 'case'.

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