[Server-devel] Fixing bash script bogosity - help?
pgf at laptop.org
pgf at laptop.org
Tue Apr 28 20:28:56 EDT 2009
andrew wrote:
> On Tue, 2009-04-28 at 23:25 +1200, Andrew McMillan wrote:
> > Hi Martin,
> >
> > Perhaps:
> >
> > =========================================
> > #!/bin/bash -x
> >
> > [ -n "$DEBUG" ] && set -o xtrace
> >
> > declare -a inparms
> >
> > declare -i i=0
> > while true; do
> > inparms[$i]="${1/\"/\\\"}"
>
> Sorry, that should have read:
>
> inparms[$i]="${1//\"/\\\"}"
>
> so that all instances of " were replaced by \", rather than just the
> first one :-)
i went down a similar path and even sent martin a modified
version of his script before realizing the general case is much
worse than that. you have to deal with escaping existing
backslashes, and single quotes, and i think all of the other
special shell characters as well.
the one bright spot of the exercise (if you can call it that) was
that in re-learing the pattern subustitution thing, i found that
you can do that "ugly hack" pattern replacement without the
surrounding loop, because it can be applied to all elements of an
array at once:
newARGS=( "${ARGS[@]//\"/\\\"}" )
small pleasures, i know.
(i didn't retest that, but it's close.)
paul
>
> Cheers,
> Andrew.
>
> > shift || break
> > i=$(( $i + 1 ))
> > done
> >
> > printargs() {
> > local -i j=0
> > while [ $j -lt $i ] ; do
> > printf ' "%s"' "${inparms[$j]}"
> > j=$(( $j + 1 ))
> > done
> > }
> >
> > # in the script, the CMD is built up as a string
> > CMD="touch `printargs`"
> >
> > # in practice we somtimes use /sbin/runuser -c
> > # and other times plain bash -c
> > bash -c "$CMD"
> >
> > =========================================
> >
> > Line 9 is an ugly hack, and quite possibly a bashism.
> >
> >
> > Hope this is some use,
> >
> > Andrew McMillan.
> >
> >
> > On Mon, 2009-04-27 at 22:37 +0200, Martin Langhoff wrote:
> > > Hi all,
> > >
> > > I have a simple shell scripting problem :-) you'll find attached a
> > > shell script that ships with ejabberd. It is a fairly straightforward
> > > bit of code, and allows us to control bits of the ejabberd internals
> > > with a nice cli interface. (Feel free to skip the start / stop bits of
> > > the code, I'm fighting with the ctrl function.)
> > >
> > > The problem it has is that the parameters are passed to a bash or
> > > runas invocation -- at which point the quoting is a mess. Currently I
> > > am working around it in the caller by doing some stupid
> > > nested-quoting. But this should be easy to cure -- if anyone knows a
> > > bit more bash (or portable shell!) than me :-)
> > >
> > > A minimal exposition of the problem is as follows:
> > >
> > > $ cat sample.sh
> > > #!/bin/bash -x
> > >
> > > # in the script, the CMD is built up as a string
> > > CMD="touch $@"
> > > # in practice we somtimes use /sbin/runuser -c
> > > # and other times plain bash -c
> > > bash -c "$CMD"
> > >
> > > # this invokation does the wrong thing -
> > > $ ./sample.sh ./sample.sh "this is file one" "this is file two"
> > > # the ugly workaround is
> > > ./sample.sh "'this is file one'" "'this is file two'"
> > >
> > > Any hints that don't involve a rewrite?
> > >
> > > cheers,
> > >
> > >
> > >
> > > martin-who's-easily-stumped-with-shell-backwardnesss
> >
> > ------------------------------------------------------------------------
> > andrew (AT) morphoss (DOT) com +64(272)DEBIAN
> > You will be awarded a medal for disregarding safety in saving someone.
> > ------------------------------------------------------------------------
> >
> >
> > _______________________________________________
> > Server-devel mailing list
> > Server-devel at lists.laptop.org
> > http://lists.laptop.org/listinfo/server-devel
> >
> ------------------------------------------------------------------------
> andrew (AT) morphoss (DOT) com +64(272)DEBIAN
> You have the power to influence all with whom you come in contact.
> ------------------------------------------------------------------------
>
>
> _______________________________________________
> Devel mailing list
> Devel at lists.laptop.org
> http://lists.laptop.org/listinfo/devel
=---------------------
paul fox, pgf at laptop.org
More information about the Server-devel
mailing list