bashgetopts

bash long options weird behavior - just for curiosity


I have this bash script, which does something after getting parameters. What I´m curious about is why it also accepts "--env, --envi, ..." when I just specified "environment" as valid option. Can I somehow lower its tolerance to only accept the whole word?

.......

args=$(getopt -a -o e: --long 'help,environment:' -- "$@")

if [[ $? -gt 0 ]]; then
  usage
fi

eval set -- ${args}

while :
do
  case $1 in
    -e | --environment)
         {
         case $2 in
                 PROD) CHECK_FILE=${NFS_MOUNT_PROD}/${FILE_MONITORING}; break ;;
                 INT) CHECK_FILE=${NFS_MOUNT_INT}/${FILE_MONITORING}; break ;;
                 *) usage; exit1;;
         esac
         }
         shift 2  ;;
    --help) usage                            ; shift   ;;
    # -- means the end of the arguments; drop this, and break out of the while loop
    --) shift; break ;;
    *) >&2 echo Unsupported option: $1
       usage ;;
  esac
done

.......

Thanks guys and greetings,

JP


Solution

  • I looked through the man page for getopt (or man getopt) and found the following:

    Long options may be abbreviated, as long as the abbreviation is not ambiguous.

    Here is a decent answer thread on using getopt vs getops.