Optional option argument with getopts

前端 未结 11 1318
while getopts \"hd:R:\" arg; do
  case $arg in
    h)
      echo \"usgae\" 
      ;;
    d)
      dir=$OPTARG
      ;;
    R)
      if [[ $OPTARG =~ ^[0-9]+$ ]];then         


        
11条回答
  •  忘掉有多难
    2020-11-28 09:08

    getopts doesn't really support this; but it's not hard to write your own replacement.

    while true; do
        case $1 in
          -R) level=1
                shift
                case $1 in
                  *[!0-9]* | "") ;;
                  *) level=$1; shift ;;
                esac ;;
            # ... Other options ...
            -*) echo "$0: Unrecognized option $1" >&2
                exit 2;;
            *) break ;;
        esac
    done
    

提交回复
热议问题