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

问题描述

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

./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 "$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

脚本的结果是:

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

托托

--删除数据

--删除配置

抑制 en cours 数据

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

我不明白我在使用 shift 和 getopts 时出了什么问题

解决方法

版本略有不同:

#!/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
,

无效的解决方案,它只适用于这种情况,不适用于 3 个子参数

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

OPTS=`getopt -o a?d:h?dd?dc? --long apps,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"
        echo "$3"
        while true; do
        case "$1" in
            -dd | --delete-data ) 
                deleteData
                shift 
                ;;
            -dc | --delete-config ) 
                deleteConfig
                shift
                break
                ;;   
            * ) 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