1

I would like to be able to run a script like this ( with sub-arguments ): I need to be able to use short and long options

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

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

./myscript.sh -h

Actually i have :


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

The result of the script is :

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

toto

--delete-data

--delete-config

suppress en cours data

Unexpected option: --delete-config - this should not happen.

I don't understand what i'm making wrong with shift and getopts

5
  • Does it accept sub-arguments with double hypens? I'd guess that the last *) prints the error message. Commented Mar 11, 2021 at 16:57
  • I can produce a case where the last sub argument works but not the first sub argument In the delete -> shift 3 Commented Mar 11, 2021 at 17:00
  • You need a while loop after this line : # Here i can add argument to the command Commented Mar 11, 2021 at 17:03
  • @Philippe Purpose your solution in answer ( this script can be tested on your side if needed without problem ) Commented Mar 11, 2021 at 17:06
  • @Philippe With just a loop, it's starting the two scripts but throw error : suppress en cours data suppress en cours config Unexpected option: --delete-config - this should not happen. Commented Mar 11, 2021 at 17:21

2 Answers 2

1

Slightly different version :

#!/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
Sign up to request clarification or add additional context in comments.

1 Comment

i confirm that your version works, not mine. Mine was only working on my case not with a third sub parameter, your yes. thanks !
0

Not working solution, it only works in this case, not with 3 sub parameters

Thanks to Philippe for his comment : Let me know if you think it's not ok, or purpose better and a confirm as answer :)

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"
        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

1 Comment

Not working solution, it only works in this case, not with 3 sub parameters

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.