zsh-users
 help / color / mirror / code / Atom feed
* Can I do without "eval" here?
@ 2006-03-14  9:03 Com MN PG P E B Consultant 3
  2006-03-14  9:16 ` DervishD
  0 siblings, 1 reply; 4+ messages in thread
From: Com MN PG P E B Consultant 3 @ 2006-03-14  9:03 UTC (permalink / raw)
  To: zsh-users Mailinglist

Maybe just a matter of style, but here is a (simplified) version of a
problem which
I have solved, but where I am not satisfied with the solution ...

Assume I have a command CMD which can be called in the following ways:

  CMD -x FILENAME
  CMD -y 'this is some string with alphanumerics and spaces' FILENAME

I call this CMD from a skript MYSCRIPT. If the skript is called with 1
parameter, I use the second form and put the
parameter into the argument of my -y option. If MYSCRIPT is called
without parameters, I use the -x option.
For sake of simplicity, we can assume that the FILENAME is always
hardcoded. That is, if someone calls
MYSCRIPT without any arguments, i.e.

  MYSCRIPT

it should execute

  CMD -x foo

But if MYSCRIPT is called like this:

  MYSCRIPT "bar baz"

it should execute 

  CMD -y 'bar baz' foo

Here is my solution (which I ask you to improve):

#!/bin/zsh
if [[ $# -eq 0 ]]
then
  options=-x
else
  options="-y '$1'"
fi
eval CMD $options foo

The 'eval' is necessary here, because if I write just

   CMD $options foo

CMD would see for example -y 'bar baz' as first argument, and not just
-y.

What I don't like in this example, is the usage of eval. Although it is
harmless in this particular case,
the filename 'foo', which is hardcoded here, comes in as a parameter too
in my real application, and 
this means that it undergoes one level of evaluation too, which would
yield wrong results when it contains,
say, a $ character. 

Of course one might argued that when you are thinking of filenames with
$, you are doomed anyway, but 
still I'm not a big fan of using 'eval' anyway, so I wonder whether this
can be done without eval
too....

Ronald


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Can I do without "eval" here?
  2006-03-14  9:03 Can I do without "eval" here? Com MN PG P E B Consultant 3
@ 2006-03-14  9:16 ` DervishD
  2006-03-14  9:22   ` Michal Politowski
  0 siblings, 1 reply; 4+ messages in thread
From: DervishD @ 2006-03-14  9:16 UTC (permalink / raw)
  To: Com MN PG P E B Consultant 3; +Cc: zsh-users Mailinglist

    Hi Ronald :)

 * Com MN PG P E B Consultant 3 <mn-pg-p-e-b-consultant-3.com@siemens.com> dixit:
> What I don't like in this example, is the usage of eval. Although
> it is harmless in this particular case, the filename 'foo', which
> is hardcoded here, comes in as a parameter too in my real
> application, and this means that it undergoes one level of
> evaluation too, which would yield wrong results when it contains,
> say, a $ character.

    Try this:

    eval whatevercommand \''$file'\'

    As for doing the same without eval, it can be done, too, by
setting the sh_word_split option and tweaking, or simply with this:

    options=("-x")
    (($#)) && options=("-y" "$1")
    CMD $options[1] $options[2] foo

    If "foo" comes from a parameter, just use that parameter instead
of "foo".

    I haven't tested the solution, so it may need some refinement,
but it should do.

    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736 | http://www.dervishd.net
http://www.pleyades.net & http://www.gotesdelluna.net
It's my PC and I'll cry if I want to... RAmen!


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Can I do without "eval" here?
  2006-03-14  9:16 ` DervishD
@ 2006-03-14  9:22   ` Michal Politowski
  2006-03-14  9:42     ` DervishD
  0 siblings, 1 reply; 4+ messages in thread
From: Michal Politowski @ 2006-03-14  9:22 UTC (permalink / raw)
  To: zsh-users Mailinglist

On Tue, 14 Mar 2006 10:16:19 +0100, DervishD wrote:
[...]
>     options=("-x")
>     (($#)) && options=("-y" "$1")
>     CMD $options[1] $options[2] foo

There is no need to split the array manually.
CMD $options foo
will do.

-- 
Michał Politowski
Talking has been known to lead to communication if practiced carelessly.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Can I do without "eval" here?
  2006-03-14  9:22   ` Michal Politowski
@ 2006-03-14  9:42     ` DervishD
  0 siblings, 0 replies; 4+ messages in thread
From: DervishD @ 2006-03-14  9:42 UTC (permalink / raw)
  To: zsh-users Mailinglist

    Hi Michal :)

 * Michal Politowski <mpol@charybda.icm.edu.pl> dixit:
> On Tue, 14 Mar 2006 10:16:19 +0100, DervishD wrote:
> >     options=("-x")
> >     (($#)) && options=("-y" "$1")
> >     CMD $options[1] $options[2] foo
> 
> There is no need to split the array manually.
> CMD $options foo
> will do.

    Oops, my bad O:)) At first I didn't write the solution using an
array, and afterwards I just forgot that arrays don't need manual
splitting. Thanks for pointing :)

    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736 | http://www.dervishd.net
http://www.pleyades.net & http://www.gotesdelluna.net
It's my PC and I'll cry if I want to... RAmen!


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2006-03-14  9:41 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-03-14  9:03 Can I do without "eval" here? Com MN PG P E B Consultant 3
2006-03-14  9:16 ` DervishD
2006-03-14  9:22   ` Michal Politowski
2006-03-14  9:42     ` DervishD

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).