如何在 bash 中绕过以下参数的可选参数?

How to bypass an optional argument for the following argument in bash?

TABLE=`echo "" | tr '[:upper:]' '[:lower:]'`
if [  = -d ]
   then TABLE=daminundation
elif [  = -b ]
   then TABLE=burnscararea
elif [  = -r ]
   then TABLE=riverpointinundation
elif [  = " " ]
   then echo "User must input -d (daminundation), -b (burnscararea) 
   or -r (riverpointinundation)."
fi
SHAPEFILEPATH=
MERGEDFILENAME=
if [ -z  ] ; 
  then MERGEDFILENAME=merged.shp
else
  MERGEDFILENAME=
fi
COLUMNNAME=
if [ -n  ]
  then COLUMNNAME=
fi

$3 和 $4 是可选参数。但是,如果我选择不使用 $3 但我想使用 $4,它会将命令读取为 $3。被其他方法弄糊涂了,我应该怎么做才能让下一个不需要的可选命令可以被绕过?

你可能想要这个:

#!/bin/bash

while getopts ":b :d :r" opt; do
  case $opt in
    b)
      TABLE=burnscararea
      ;;
    d)
      TABLE=daminundation
      ;;
    r)
      TABLE=riverpointinundation
      ;;
    \?)
      echo "Invalid option: -$OPTARG" >&2
      exit 1
      ;;
  esac
done

shift $((OPTIND-1))

[ -z "$TABLE" ] && ( echo "At least one of -b/-d/-r options must be provided"; exit 1; )
[ $# -ne 3 ] && ( echo "3 params expected!"; exit 1; )
SHAPEFILEPATH=""
MERGEDFILENAME=""
COLUMNNAME=""
# other stuff