Fixing bash script bogosity - help?

Ignacio Vazquez-Abrams ivazqueznet at gmail.com
Tue Apr 28 06:03:19 EDT 2009


On Tue, 2009-04-28 at 11:17 +0200, Martin Langhoff wrote:
> On Tue, Apr 28, 2009 at 9:27 AM, Ignacio Vazquez-Abrams
> <ivazqueznet at gmail.com> wrote:
> >> # in the script, the CMD is built up as a string
> >> CMD="touch $@"
> >
> > http://mywiki.wooledge.org/BashFAQ/050
> 
> That repeats what we know already. The thing is that we are building
> the command as a string for runuser, which wants it as a parameter.
> 
> It is an interesting question, I think. Can you make my sample script
> work, while still handling $CMD as a string? Some sample input for the
> challenge:

No, I can't. The first section explains that it can't be done
*precisely* for the same reason you've already discovered.

But if you're willing to let go of this futile quest to keep $CMD a
string then you'll find that it can be done:

#! /bin/bash

CMD=(touch "$@")
bash -c "${CMD[@]}"

-- 
Ignacio Vazquez-Abrams <ivazqueznet at gmail.com>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part
URL: <http://lists.laptop.org/pipermail/devel/attachments/20090428/dde2f651/attachment.sig>


More information about the Devel mailing list