nmh-workers
[Top][All Lists]
Advanced

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

Re: [Nmh-workers] Quoting for test commands


From: Bill Wohler
Subject: Re: [Nmh-workers] Quoting for test commands
Date: Mon, 21 Jul 2014 18:35:31 -0700
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.4 (gnu/linux)

Ralph Corderoy <address@hidden> 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
> address@hidden
> https://lists.nongnu.org/mailman/listinfo/nmh-workers
>

-- 
Bill Wohler <address@hidden> aka <address@hidden>
http://www.newt.com/wohler/
GnuPG ID:610BD9AD




reply via email to

[Prev in Thread] Current Thread [Next in Thread]