0

This is my shell script-

while getopts ":m" opt; do
  case "$opt" in
    m)
      echo "-m was triggered! $OPTARG was entered" >&2
      ;;
    j)
       echo "-j was triggered! $4 was entered" >&2
       ;;
    k)
       echo "-k was triggered! $6 was entered" >&2
       ;;
   \?)
      echo "Invalid option: -$OPTARG" >&2
      ;;
  esac
done
shift $(($OPTIND - 1))

when i run the shell script-

./test.sh -m hello

I get the output this way-

-m was triggered!  was entered

Where am i going wrong? Please bear with me as I am totally new to shell script.

AlwaysALearner
  • 6,320
  • 15
  • 44
  • 59

1 Answers1

1

It should m: instead of :m

while getopts "m:" opt; do
Guru
  • 16,456
  • 2
  • 33
  • 46
  • What should i do to get the output as `./test.sh -mytest hello`? The option `mytest)` doesn't seem to be working with the existing code. – AlwaysALearner Dec 07 '12 at 05:08
  • @user1649068 : check this : http://stackoverflow.com/questions/402377/using-getopts-in-bash-shell-script-to-get-long-and-short-command-line-options – Guru Dec 07 '12 at 05:12