Re: [Server-devel] Fixing bash script bogosity - help?

2009-04-28 Thread Ignacio Vazquez-Abrams
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 $@

http://mywiki.wooledge.org/BashFAQ/050

-- 
Ignacio Vazquez-Abrams ivazquez...@gmail.com


signature.asc
Description: This is a digitally signed message part
___
Server-devel mailing list
Server-devel@lists.laptop.org
http://lists.laptop.org/listinfo/server-devel


Re: [Server-devel] Fixing bash script bogosity - help?

2009-04-28 Thread Ignacio Vazquez-Abrams
On Tue, 2009-04-28 at 11:17 +0200, Martin Langhoff wrote:
 On Tue, Apr 28, 2009 at 9:27 AM, Ignacio Vazquez-Abrams
 ivazquez...@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 ${c...@]}

-- 
Ignacio Vazquez-Abrams ivazquez...@gmail.com


signature.asc
Description: This is a digitally signed message part
___
Server-devel mailing list
Server-devel@lists.laptop.org
http://lists.laptop.org/listinfo/server-devel


Re: [Server-devel] Fixing bash script bogosity - help?

2009-04-28 Thread Martin Langhoff
On Tue, Apr 28, 2009 at 1:01 PM, Ignacio Vazquez-Abrams
ivazquez...@gmail.com wrote:
 Which part of the following doesn't work?

Just test your minimal 3 line script with the example input I've given
you. It breaks:

# cat  sample.sh
#! /bin/bash

CMD=(touch $@)
bash -c ${c...@]}
# ./sample.sh this is file one this is file two file

# bash -x ./sample.sh this is file one this is file two file \ f
+ CMD=(touch $@)
+ bash -c touch 'this is file one' 'this is file two' 'file  f'
touch: missing file operand
Try `touch --help' for more information.

Bash (line runuser) expects a line of shell, quoted/escaped
appropriately, as a single string param -c

*that* is the challenge.




m
-- 
 martin.langh...@gmail.com
 mar...@laptop.org -- School Server Architect
 - ask interesting questions
 - don't get distracted with shiny stuff  - working code first
 - http://wiki.laptop.org/go/User:Martinlanghoff
___
Server-devel mailing list
Server-devel@lists.laptop.org
http://lists.laptop.org/listinfo/server-devel


Re: [Server-devel] Fixing bash script bogosity - help?

2009-04-28 Thread pgf
bert wrote:
  
  On 28.04.2009, at 13:37, Martin Langhoff wrote:
  
   On Tue, Apr 28, 2009 at 1:19 PM, Ignacio Vazquez-Abrams
   ivazquez...@gmail.com wrote:
   Ah, I see now.
  
   Try this:
  
   bash -c 'touch $@' ${c...@]}
  
   Riiight, that works better... but
  
   Or in the case of the full script:
  
   bash -c $ERL' $@' ${erl_comma...@]}
  
   ...it doesn't work for runuser -- which is the real target. Runuser
   looks at the added params after -c and tries to parse them. There
   doesn't seem to be any support for passing parameters.
  
   hm.
  
  Maybe you should use su directly instead of runuser?

won't that have the same problem?  it still wants a command
passed via a -c STRING convention.

i think this is somewhat intractable, and any time spent on it
would be better spent creating a patch to runuser that lets it
take its command as strace does, or as xterm does with -e, which
avoids the vector-string-vector translations which are the real
issue.

paul
=-
 paul fox, p...@laptop.org
___
Server-devel mailing list
Server-devel@lists.laptop.org
http://lists.laptop.org/listinfo/server-devel


Re: [Server-devel] Fixing bash script bogosity - help?

2009-04-28 Thread Ignacio Vazquez-Abrams
On Tue, 2009-04-28 at 12:38 +0200, Martin Langhoff wrote:
 On Tue, Apr 28, 2009 at 12:03 PM, Ignacio Vazquez-Abrams
 ivazquez...@gmail.com wrote:
  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:
 
 Your example doesn't work. I don't care about keeping $CMD as a
 string, I think you are misunderstanding the problem. We need to build
 it as a string to provide it as a single parameter value. This is
 because we want to run the command with all properly escaped params
 under runuser.

Which part of the following doesn't work?

ctl()
{
ERL_COMMAND=(
$ERL
$NAME ejabberctl
-noinput
-pa $EJABBER_EBIN
-s ejabber_ctl -extra $ERLANG_NODE $@
)
W=`whoami`
if [ $W != ejabberd ]; then
/sbin/runuser -s /bin/bash - ejabberd -c ${erl_comma...@]}
result=$?
else
bash -c ${erl_comma...@]}
result=$?
fi

# replace remainder of function here
}

-- 
Ignacio Vazquez-Abrams ivazquez...@gmail.com


signature.asc
Description: This is a digitally signed message part
___
Server-devel mailing list
Server-devel@lists.laptop.org
http://lists.laptop.org/listinfo/server-devel


Re: [Server-devel] Fixing bash script bogosity - help?

2009-04-28 Thread Martin Langhoff
On Tue, Apr 28, 2009 at 9:27 AM, Ignacio Vazquez-Abrams
ivazquez...@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:

$ ./sample.sh ./sample.sh this is file one this is file two file
with doublequotes \ in it

cheers,



m
-- 
 martin.langh...@gmail.com
 mar...@laptop.org -- School Server Architect
 - ask interesting questions
 - don't get distracted with shiny stuff  - working code first
 - http://wiki.laptop.org/go/User:Martinlanghoff
___
Server-devel mailing list
Server-devel@lists.laptop.org
http://lists.laptop.org/listinfo/server-devel


Re: [Server-devel] Fixing bash script bogosity - help?

2009-04-27 Thread pgf
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, p...@laptop.org
___
Server-devel mailing list
Server-devel@lists.laptop.org
http://lists.laptop.org/listinfo/server-devel


Re: [Server-devel] Fixing bash script bogosity - help?

2009-04-27 Thread Sean DALY
I'm no maven, but the last time I was dealing with quoting issues with
$* expansion I had found this article helpful:
http://www.ibm.com/developerworks/library/l-bash-parameters.html?ca=drs-

Sean


On Mon, Apr 27, 2009 at 10:37 PM, Martin Langhoff
martin.langh...@gmail.com 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
 --
  martin.langh...@gmail.com
  mar...@laptop.org -- School Server Architect
  - ask interesting questions
  - don't get distracted with shiny stuff  - working code first
  - http://wiki.laptop.org/go/User:Martinlanghoff

 ___
 Server-devel mailing list
 Server-devel@lists.laptop.org
 http://lists.laptop.org/listinfo/server-devel


___
Server-devel mailing list
Server-devel@lists.laptop.org
http://lists.laptop.org/listinfo/server-devel