bash 脚本中的 Getopts 带有子参数

Getopts in bash script with sub-arguments

我希望能够 运行 这样的脚本(带有子参数): 我需要能够使用短期和长期选项

./myscript.sh -u myname --delete-config --delete-data

./myscript.sh -a myname ..........................

./myscript.sh -h

其实我有:


OPTS=`getopt -o a?d:h?dd?dc? --long apps,delete:,delete-data,delete-config -n 'parse-options' -- "$@"`

if [ $? != 0 ] ; then echo "Failed parsing options." >&2 ; exit 1 ; fi

eval set -- "$OPTS"


while true; do
  case "" in
    -a | --apps ) 
        showHelp
        exit 0
        ;;
    -d | --delete ) 
        username=""
        echo $username
        shift 2
        echo ""
        echo ""
       
        # Here i can add argument to the command

        case "" in
            -dd | --delete-data ) 
                deleteData
                shift 1
                ;;
            -dc | --delete-config ) 
                deleteConfig
                shift 1
                ;;   
            * ) echo "Unexpected option:  - this should not happen."
                showHelp 
                break 
                ;;
        esac
        ;;
    -h | --help ) 
        showHelp 
        break 
        ;;
    -- ) shift; 
        break 
        ;;    
    * ) echo "Unexpected option:  - this should not happen."
        showHelp 
        break 
        ;;
  esac
done

脚本的结果是:

--delete toto --delete-data --delete-config

多多

--删除数据

--删除配置

禁止浏览数据

意外选项:--delete-config - 这不应该发生。

我不明白我在 shift 和 getopts 上做错了什么

无效解决方案,仅在这种情况下有效,不适用于 3 个子参数

感谢 Philippe 的评论: 如果您认为这不好,或者有更好的目的并确认为答案,请告诉我:)

OPTS=`getopt -o a?d:h?dd?dc? --long apps,delete:,delete-data,delete-config -n 'parse-options' -- "$@"`

if [ $? != 0 ] ; then echo "Failed parsing options." >&2 ; exit 1 ; fi

eval set -- "$OPTS"


while true; do
  case "" in
    -a | --apps ) 
        showHelp
        exit 0
        ;;
    -d | --delete ) 
        username=""
        echo $username
        shift 2
        echo ""
        echo ""
        echo ""
        while true; do
        case "" in
            -dd | --delete-data ) 
                deleteData
                shift 
                ;;
            -dc | --delete-config ) 
                deleteConfig
                shift
                break
                ;;   
            * ) echo "Unexpected option:  - this should not happen."
                showHelp 
                break 
                ;;
        esac
        done
        ;;
    -h | --help ) 
        showHelp 
        break 
        ;;
    -- ) shift; 
        break 
        ;;    
    * ) echo "Unexpected option:  - this should not happen."
        showHelp 
        break 
        ;;
  esac
done

略有不同的版本:

#!/usr/bin/env bash

OPTS=`getopt -o a?d:h?dd?dc? --long apps,delete:,delete-data,delete-config -n 'parse-options' -- "$@"`

if [ $? != 0 ] ; then echo "Failed parsing options." >&2 ; exit 1 ; fi

eval set -- "$OPTS"


while test "" != --; do
  case "" in
    -a | --apps )
        showHelp
        exit 0
        ;;
    -d | --delete )
        username=""
        echo $username
        shift 2
        echo ""
        echo ""

        # Here i can add argument to the command

        while test "" != --; do
            case "" in
                -dd | --delete-data )
                    echo deleteData
                    shift 1
                    ;;
                -dc | --delete-config )
                    echo deleteConfig
                    shift 1
                    ;;
                * ) echo "Unexpected option:  - this should not happen."
                    showHelp
                    break
                    ;;
            esac
        done
        ;;
    -h | --help )
        showHelp
        break
        ;;
    -- ) shift;
        break
        ;;
    * ) echo "Unexpected option:  - this should not happen."
        showHelp
        break
        ;;
  esac
done