Bash編程 - getopt

Bash script that demonstrates the usage of getopt

http://linuxwell.com/2011/07/14/getopt-in-bash/


#!/bin/bash

# Execute getopt
ARGS=`getopt -o "123:" -l "one,two,three:" \
      -n "getopt.sh" -- "$@"`

#Bad arguments
if [ $? -ne 0 ];
then
  exit 1
fi

# A little magic
eval set -- "$ARGS"

# Now go through all the options
while true;
do
  case "$1" in
    -1|--one)
      echo "Uno"
      shift;;

    -2|--two)
      echo "Dos"
      shift;;

    -3|--three)
      echo "Tres"

      # We need to take the option argument
      if [ -n "$2" ];
      then
        echo "Argument: $2"
      fi
      shift 2;;

    --)
      shift
      break;;
  esac
done




你可能感兴趣的:(Bash編程 - getopt)