1

Possible Duplicate:
Using getopts in bash shell script to get long and short command line options

This is the code i've written

#! /bin/bash

getopts master $1

while getopts ":master" opt; do
  case $opt in
    master)
      echo "-master was triggered! $1 was entered" >&2
      ;;
    \?)
      echo "Invalid option: -$OPTARG" >&2
      ;;
  esac
done

and this is the output i'm getting-

]$ ./test123.sh -master 123
./test123.sh: line 3: getopts: `-master': not a valid identifier

How do i define a user defined option?

Community
  • 1
  • 1
AlwaysALearner
  • 6,320
  • 15
  • 44
  • 59

1 Answers1

3

I think there are a couple of problems.

  1. I don't understand what this (getopts master $1) is doing and you can remove it
  2. I think you need single char arguments (i.e. -m vs -master)

e.g. the below seems to work:

#!/bin/bash
while getopts ":m" opt; do
  case $opt in
    m)
      echo "-m was triggered! $1 was entered" >&2
      ;;
    \?)
      echo "Invalid option: -$OPTARG" >&2
      ;;
  esac
done
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440