[Top] [All Lists]

Re: [Nmh-workers] Quoting for test commands

2013-11-14 05:55:48
Hi Ken,

I'm having a heck of a time figuring out how to do shell quoting right
with command substitution.

As Lyndon hinted, you need the shell to do an extra level of

(Each "word" interpreted as an individual argument).  I've played
around with single quotes, double quotes, backslashes, and clearly I'm
missing something.

Yes, it can't be done like that.

    $ cat ken
    #! /bin/sh

    run_test() {
        actual_output=`$1 2>&1`
        echo actual_output: $actual_output

    run_test2() {
        actual_output=`eval $1 2>&1`
        echo actual_output: $actual_output

    fmttest() {
        shift 3
        printf '<%s>\n' "$@"

    fmttest -raw -format '%(unquote)' "Mr. Foo Bar"
    run_test 'fmttest -raw -format "%(unquote)" "Mr. Foo Bar"'
    run_test 'eval fmttest -raw -format "%(unquote)" "Mr. Foo Bar"'
    run_test2 'fmttest -raw -format "%(unquote)" "Mr. Foo Bar"'
    $ ./ken
    <Mr. Foo Bar>
    actual_output: <"Mr.> <Foo> <Bar">
    actual_output: <Mr. Foo Bar>
    actual_output: <Mr. Foo Bar>

In your run_test, $1 is being replaced with your whole fmttest command,
including all its arguments, but double-quote processing doesn't then
occur;  it already has and you've missed the boat.  You need instead to
be prepared for two levels of interpretation and request another with
eval.  Altering run_test to have the eval, like run_test2, might not be
the correct fix though because some of your calls might not expect that
extra level and need additional quoting.  The alternative is to add the
eval to the run_test call just when it's needed, like my third example.

Cheers, Ralph.

Nmh-workers mailing list

<Prev in Thread] Current Thread [Next in Thread>