Monday, November 12, 2012

unix - Using getopts in bash shell script to get long and short command line options - Stack Overflow

unix - Using getopts in bash shell script to get long and short command line options - Stack Overflow
** "getopts": with short options AND long options AND short/long arguments **
Works with all combinations, e.G.:
  • foobar -f --bar
  • foobar --foo -b
  • foobar -bf --bar --foobar
  • foobar -fbFBAshorty --bar -FB --arguments=longhorn
  • foobar -fA "text shorty" -B --arguments="text longhorn"
  • bash foobar -F --barfoo
  • sh foobar -B --foobar - ...
  • bash ./foobar -F --bar

#!/bin/bash
# foobar: getopts with short and long options AND arguments
###### some declarations for these example ######
Options=$@
Optnum=$#
sfoo='no '
sbar='no '
sfoobar='no '
sbarfoo='no '
sarguments='no '
sARG=empty
lfoo='no '
lbar='no '
lfoobar='no '
lbarfoo='no '
larguments='no '
lARG=empty

_usage() {
###### U S A G E : Help and ERROR ######
cat <<EOF
 foobar $Options
$*
        Usage: foobar <[options]>
        Options:
                -b   --bar            Set bar to yes    ($foo)
                -f   --foo            Set foo to yes    ($bart)
                -h   --help           Show this message
                -A   --arguments=...  Set arguments to yes ($arguments) AND get ARGUMENT ($ARG)
                -B   --barfoo         Set barfoo to yes ($barfoo)
                -F   --foobar         Set foobar to yes ($foobar)
EOF
}
if [ $# = 0 ]; then _usage "  >>>>>>>> no options given "; fi
##################################################################    
#######  "getopts" with: short options  AND  long options  #######
#######            AND  short/long arguments               #######
while getopts ':bfh-A:BF' OPTION ; do
  case "$OPTION" in
    b  ) sbar=yes                       ;;
    f  ) sfoo=yes                       ;;
    h  ) _usage                         ;;   
    A  ) sarguments=yes;sARG="$OPTARG"  ;;
    B  ) sbarfoo=yes                    ;;
    F  ) sfoobar=yes                    ;;
    -  ) [ $OPTIND -ge 1 ] && optind=$(expr $OPTIND - 1 ) || optind=$OPTIND
         eval OPTION="\$$optind"
         OPTARG=$(echo $OPTION | cut -d'=' -f2)
         OPTION=$(echo $OPTION | cut -d'=' -f1)
         case $OPTION in
             --foo       ) lfoo=yes                       ;;
             --bar       ) lbar=yes                       ;;
             --foobar    ) lfoobar=yes                    ;;
             --barfoo    ) lbarfoo=yes                    ;;
             --help      ) _usage                         ;;
             --arguments ) larguments=yes;lARG="$OPTARG"  ;; 
             * )  _usage " Long: >>>>>>>> invalide options (long) " ;;
         esac
       OPTIND=1
       shift
      ;;
    ? )  _usage "Short: >>>>>>>> invalide options (short) "  ;;
  esac
done
##################################################################
echo "----------------------------------------------------------"
echo "RESULT short-foo      : $sfoo                                 long-foo      : $lfoo"
echo "RESULT short-bar      : $sbar                                 long-bar      : $lbar"
echo "RESULT short-foobar   : $sfoobar                                 long-foobar   : $lfoobar"
echo "RESULT short-barfoo   : $sbarfoo                                 long-barfoo   : $lbarfoo"
echo "RESULT short-arguments: $sarguments  with Argument = \"$sARG\"        long-arguments: $larguments and $lARG"
share|edit
Was this post useful to you?     

No comments: