nmh-workers
[Top] [All Lists]

Re: [Nmh-workers] Quoting for test commands

2014-07-21 20:36:06
Ralph Corderoy <ralph(_at_)inputplus(_dot_)co(_dot_)uk> writes:

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
interpretation.

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

Hey, I just had this very same problem in one of my scripts.

    actual_output: <Mr. Foo Bar>

Thanks very much for showing us the way! I thought I had tried eval, but
will try again with this example.

    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
Nmh-workers(_at_)nongnu(_dot_)org
https://lists.nongnu.org/mailman/listinfo/nmh-workers


-- 
Bill Wohler <wohler(_at_)newt(_dot_)com> aka 
<Bill(_dot_)Wohler(_at_)nasa(_dot_)gov>
http://www.newt.com/wohler/
GnuPG ID:610BD9AD


_______________________________________________
Nmh-workers mailing list
Nmh-workers(_at_)nongnu(_dot_)org
https://lists.nongnu.org/mailman/listinfo/nmh-workers

<Prev in Thread] Current Thread [Next in Thread>
  • Re: [Nmh-workers] Quoting for test commands, Bill Wohler <=