0

This is the code I'm working with:

TYPE=""
FILE=""

while getopts "t:f:" opt; do
    case $opt in
        t)  TYPE="$OPTARG"
            ;;
        f)  FILE="$OPTARG"
            ;;
    esac
done

if [ -z "$TYPE" ]; then
  echo "No -t. Bye."
  exit 1 # error
else
  if [ -n "$FILE" ]; then
    echo "$TYPE and $FILE"
  else
    echo JUST $TYPE
  fi
fi

Is it possible to specify valid options for $TYPE? For example valid type options are:

IMAGE, ZIP, DOC

If one of these types are specified as valid arguments then the script runs the existing line:

"echo "$TYPE and $FILE""

Otherwise it echos an error and quits. Is this possible to do?

Jimmy
  • 12,087
  • 28
  • 102
  • 192

1 Answers1

3

If you need to filter out -t switch :

(...)
        t)
            case $OPTARG in
                img|image|doc)
                    TYPE="$OPTARG"
                ;;
                *)
                    echo >&2 "Unsupported type..."
                    exit 1
                ;;
            esac
        ;;
(...)
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
  • 1
    probably don't want to `break` -- `t) case $OPTARG in (img|image|doc) TYPE=$OPTARG ;; ...` might be better – glenn jackman Jan 22 '13 at 21:39
  • Thank you for the reply. I'm a little confused by your example, do I still need the while getopts "t:f:" opt; do line? – Jimmy Jan 22 '13 at 22:18
  • I have guessed how to implement it here but I'm worried I have misunderstood https://gist.github.com/4599218 – Jimmy Jan 22 '13 at 22:26