Linux 带有子参数的bash脚本中的Getopts

Linux 带有子参数的bash脚本中的Getopts,linux,bash,getopt,getopts,Linux,Bash,Getopt,Getopts,我希望能够运行这样的脚本(带有子参数): 我需要能够使用短期和长期选项 /myscript.sh-u myname--delete config--delete data /myscript.sh-我的名字 /myscript.sh-h 事实上,我有: OPTS=`getopt -o a?d:h?dd?dc? --long apps,delete:,delete-data,delete-config -n 'parse-options' -- "$@"` if [ $?

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

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

/myscript.sh-我的名字

/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 "$1" in
    -a | --apps ) 
        showHelp
        exit 0
        ;;
    -d | --delete ) 
        username="$2"
        echo $username
        shift 2
        echo "$1"
        echo "$2"
       
        # Here i can add argument to the command

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

--删除toto--删除数据--删除配置

托托

--删除数据

--删除配置

抑制过程数据

意外选项:---删除配置-不应发生这种情况


我不明白shift和getopts的错误是什么,解决方案不起作用,它只在这种情况下起作用,没有3个子参数

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


稍有不同的版本:

#!/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 "$1" != --; do
  case "$1" in
    -a | --apps )
        showHelp
        exit 0
        ;;
    -d | --delete )
        username="$2"
        echo $username
        shift 2
        echo "$1"
        echo "$2"

        # Here i can add argument to the command

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

它接受带有双重催眠的子参数吗?我猜最后一个*)会打印错误消息。我可以生成一个案例,其中delete->shift 3中的最后一个子参数有效,但不是第一个子参数。您需要一个
,而这行后面的
循环:
#这里我可以在命令中添加参数
@Philippe Purpose您的解决方案作为答案(如果需要,可以在您这边测试此脚本,不会出现问题)@Philippe只是一个循环,它正在启动两个脚本,但抛出错误:suppress en cours data suppress en cours config意外选项:-delete config-这不应该发生。不起作用的解决方案,它只在这种情况下起作用,没有3个子参数。我确认你的版本起作用,没有我的版本。我的版本只在我的情况下起作用,没有使用third子参数,您的是。谢谢!
#!/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 "$1" != --; do
  case "$1" in
    -a | --apps )
        showHelp
        exit 0
        ;;
    -d | --delete )
        username="$2"
        echo $username
        shift 2
        echo "$1"
        echo "$2"

        # Here i can add argument to the command

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