Bash getopts: lire $ OPTARG pour les drapeaux optionnels?

J'aimerais être en mesure d'accepter à la fois obligatoires et facultatives les drapeaux dans mon script. Voici ce que j'ai jusqu'à présent.

#!bin/bash

while getopts ":a:b:cdef" opt; do
      case $opt in
        a ) APPLE="$OPTARG";;
        b ) BANANA="$OPTARG";;
        c ) CHERRY="$OPTARG";;
        d ) DFRUIT="$OPTARG";;
        e ) EGGPLANT="$OPTARG";;
        f ) FIG="$OPTARG";;
        \?) echo "Invalid option: -"$OPTARG"" >&2
            exit 1;;
        : ) echo "Option -"$OPTARG" requires an argument." >&2
            exit 1;;
      esac
    done
echo "Apple is "$APPLE""
echo "Banana is "$BANANA""
echo "Cherry is "$CHERRY""
echo "Dfruit is "$DFRUIT""
echo "Eggplant is "$EGGPLANT""
echo "Fig is "$FIG""

Toutefois, la sortie de la suivante:

bash script.sh -a apple -b banana -c cherry -d dfruit -e eggplant -f fig

...sorties ceci:

Apple is apple
Banana is banana
Cherry is 
Dfruit is 
Eggplant is 
Fig is

Comme vous pouvez le voir, les indicateurs facultatifs ne sont pas en tirant les arguments $OPTARG comme il le fait avec les drapeaux. Est-il un moyen de lire $OPTARG sur les indicateurs facultatifs sans se débarrasser de la nlfa ":)" erreur de manipulation?

=======================================

EDIT: je me suis retrouvé en suivant les conseils de Gilbert ci-dessous. Voici ce que j'ai fait:

#!/bin/bash

  if [[ "$1" =~ ^((-{1,2})([Hh]$|[Hh][Ee][Ll][Pp])|)$ ]]; then
    print_usage; exit 1
  else
    while [[ $# -gt 0 ]]; do
      opt="$1"
      shift;
      current_arg="$1"
      if [[ "$current_arg" =~ ^-{1,2}.* ]]; then
        echo "WARNING: You may have left an argument blank. Double check your command." 
      fi
      case "$opt" in
        "-a"|"--apple"      ) APPLE="$1"; shift;;
        "-b"|"--banana"     ) BANANA="$1"; shift;;
        "-c"|"--cherry"     ) CHERRY="$1"; shift;;
        "-d"|"--dfruit"     ) DFRUIT="$1"; shift;;
        "-e"|"--eggplant"   ) EGGPLANT="$1"; shift;;
        "-f"|"--fig"        ) FIG="$1"; shift;;
        *                   ) echo "ERROR: Invalid option: \""$opt"\"" >&2
                              exit 1;;
      esac
    done
  fi

  if [[ "$APPLE" == "" || "$BANANA" == "" ]]; then
    echo "ERROR: Options -a and -b require arguments." >&2
    exit 1
  fi

Merci beaucoup à tout le monde. Cela fonctionne parfaitement jusqu'à présent.

source d'informationauteur earth | 2013-08-24