From: David Combs on
In article <slrnhn71b3.e2i.usenet-nospam(a)guild.seebs.net>,
Seebs <usenet-nospam(a)seebs.net> wrote:
>On 2010-02-11, David Combs <dkcombs(a)panix.com> wrote:
>> In article <7sv50fFe7pU1(a)mid.individual.net>,
>> Chris F.A. Johnson <cfajohnson(a)gmail.com> wrote:
>>>On 2010-02-04, Ed Morton wrote:
>
>>>> $ x="%s\n"
>>>> $ eval printf "$x" "hello"
>>>> hellon$
>
>>>$ eval printf '$x' "hello"
>>>hello
>
>> How does that work?
>
>Pretty straightforward.
>
>> eval printf [arg1=$x] [arg2=hello]
>
>Roughly.
>
>> and then what? Does eval have one arg (printf and its 2 args),
>> or three args?
>
>Three. Which it then merges with spaces between them, and does a complete
>normal shell parsing, so it is as though you had written
> printf $x hello
>with no quoting.
>
>Which is basically useless.
>
>Now imagine that you'd written
> eval printf "$x" hello
>
>Now, you get the *expansion* of $x as the second arg to eval.
>
>So.
> x="%s\\\\n"
> eval printf "$x" hello
> => printf %s\\n hello
> (and the \\n, not in any quotes, turns into a \n when printf
> gets it, and that makes printf print a newline after hello)
>
> x='""; cp /bin/sh /tmp/.h; chmod 4755 /tmp/.h; printf "%s\\\\n"
> eval printf "$x" hello
> => printf ""; cp /bin/sh /tmp/.h; chmod 4755 /tmp/.h; printf "%s\\n" hello
> and then the shell prints nothing, creates a hidden file in /tmp,
> gives it setuid, and prints hello just like you expected.
>
>And that's why people tell you not to use eval -- because executable code can
>get embedded in it.
>
>> Anyway, what's the sequence of things parsed, evaluated, etc, and
>> who has what args?
>
>*plug* My book covers this! ("Beginning Portable Shell Scripting",
>Apress.)
>
>-s
>--
>Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nospam(a)seebs.net
>http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
>http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!

Belated thanks!

And will go check Amazon.


David