Fixing bash script bogosity - help?

pgf at laptop.org pgf at laptop.org
Mon Apr 27 17:34:08 EDT 2009


hi martin --

 > 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"

first, you want to preserve the original quoting of the args by
using "$@".  it must look just like that.  second, you don't need
the "bash -c" there.  just run $CMD directly.  so, the invocation
you want is:
    CMD=touch
    $CMD "$@"

in your original script looks like this

    ERL_COMMAND="$ERL \
      $NAME ejabberdctl \
      -noinput \
      -pa $EJABBERD_EBIN \
      -s ejabberd_ctl -extra $ERLANG_NODE $@ \
      "
    W=`whoami`
    if [ "$W" != "ejabberd" ]; then
	/sbin/runuser -s /bin/bash - ejabberd -c "$ERL_COMMAND"
	result=$?
    else
	bash -c "$ERL_COMMAND"
	result=$?
    fi

a) remove $@ from the ERL_COMMAND definition
b) change the "bash -c" line to be:
    $ERL_COMMAND "$@"
c) to fix the runuser invocation (assuming it's broken, and i guess
    it probably is), i think will be trickier.  i'm sure we can fix
    it though.

how's this so far?

paul
=---------------------
 paul fox, pgf at laptop.org



More information about the Devel mailing list