Question

I'm trying to pass an argument as "verbatim" to my script, the problem is that the argument in inside double quotes "" and it starts with a double dash/double hyphen --.

This is an example

script.sh -f "--conf=bla"

In my script both $* and $@ transform this into

-f --conf=bla

and when this thing reaches getopts there is no way to decode this information the way it should be.

And by the way this is my getopts

foo()
{
    while getopts ":f:" vars
    do
            case ${vars} in 
                    f ) MYVAR=${OPTARG};;  
                    * ) Err; exit 1;;
            esac
    done
    shift $((OPTIND-1))
}

I would like to store --conf=bla inside MYVAR, I can't find a way to do this, apparently I can't control the way the double quotes are stripped away and, in general, I can't really pass text as verbatim to my script.

How I can control this ?

Was it helpful?

Solution

$ cat t.sh
#!/bin/sh

foo()
{
        # unset OPTIND
        while getopts ":f:" vars
        do
                case ${vars} in
                        f) MYVAR=${OPTARG} ;;
                        *) echo "error"; exit 1 ;;
                esac
        done
        shift $((OPTIND-1))

        echo "\$MYVAR=[${MYVAR}]"
}

foo "$@"

.

$ ./t.sh -f "--conf=blah"
$MYVAR=[--conf=blah]

Please elaborate?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top