Using getopts to process long and short command line options

后端 未结 30 1638
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-21 22:52

I wish to have long and short forms of command line options invoked using my shell script.

I know that getopts can be used, but like in Perl, I have not

30条回答
  •  臣服心动
    2020-11-21 23:06

    #!/bin/bash
    while getopts "abc:d:" flag
    do
      case $flag in
        a) echo "[getopts:$OPTIND]==> -$flag";;
        b) echo "[getopts:$OPTIND]==> -$flag";;
        c) echo "[getopts:$OPTIND]==> -$flag $OPTARG";;
        d) echo "[getopts:$OPTIND]==> -$flag $OPTARG";;
      esac
    done
    
    shift $((OPTIND-1))
    echo "[otheropts]==> $@"
    
    exit
    

    .

    #!/bin/bash
    until [ -z "$1" ]; do
      case $1 in
        "--dlong")
          shift
          if [ "${1:1:0}" != "-" ]
          then
            echo "==> dlong $1"
            shift
          fi;;
        *) echo "==> other $1"; shift;;
      esac
    done
    exit
    

提交回复
热议问题