[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Bash variable escaping



Denis Witt <denis.witt@concepts-and-training.de> wrote:
> I've a problem with a script. It's a wrapper for a program which uses
> for example '*' as a parameter. It could also be 'foobar*' [...]

Do you expect the program to see the asterisk character itself, or an
expansion into the corresponding list of files in the current folder?

    #!/bin/sh
    read X; echo "X=> $X <"

If you enter a single asterisk ('*'), that's what X will be set to.

    #!/bin/sh
    echo "*=> $* <"

If you supply an asterisk on the command line, the shell (not your
program) will expand that to match files in the current folder.

So if you're expecting your dialog --inputbox to accept a single
character asterisk ('*') or a shell pattern such as foobar* then that's
what it will pass to your program. It won't match against the files in
the current folder because that's not a function of the dialog program -
it's a function of the shell.

Apologies if this is blindingly obvious.

You either need to eval the value returned from dialog --inputbox or
else ensure that you DO NOT quote it when you use it as a command line.


> The parameter is set correctly: "echo ${parameter}" gave me exactly what
> I expect to see ('*')

Ah. Not necessarily. You've not quoted the variable, so it's liable for
filename matching. But if the shell cannot expand the pattern then it
leaves the pattern alone. See this example:

    mkdir /tmp/t && cd /tmp/t
    touch a1 a2 b1 b2
    X='a*'
    echo $X
    X='d*'
    echo $X

    mkdir x && cd x
    X='*'
    echo $X
    touch a b c
    echo $X


> Unfortunately when the parameter is passed to the
> program it looks like this (according to "set +x"): 

> /usr/bin/salt ''\''*'\''' state.highstate -v test=True -b 10

> How can I avoid that?

This should work (notice the lack of quotes around the variable),
assuming the script is running in the folder that the user expects:

    /usr/bin/salt $parameter state.highstate -v test=True -b 10

Chris


Reply to: