
On 2012-05-02 16:49, Chris Samuel wrote:
Hi folks,
I'm banging my head against how to properly pass a list of arguments in a variable through to a command in bash where the list of arguments itself has quoted values in it.
Here's a test case script:
$ cat odd-quoting.sh #!/bin/bash
a='--debug --make-maker=--make-maker="INSTALLDIRS=vendor INSTALLMAN1DIR=none INSTALLMAN3DIR=none" --no-sign'
echo cpan2rpm $a
If I run that script with dash on Ubuntu it looks fine:
$ sh -x odd-quoting.sh + a=--debug --make-maker=--make-maker="INSTALLDIRS=vendor INSTALLMAN1DIR=none INSTALLMAN3DIR=none" --no-sign + echo cpan2rpm --debug --make-maker=--make-maker="INSTALLDIRS=vendor INSTALLMAN1DIR=none INSTALLMAN3DIR=none" --no-sign [...]
But if I run it with bash on either Ubuntu or my actual target, RHEL6, it gets it wrong:
$ bash -x odd-quoting.sh + a='--debug --make-maker=--make-maker="INSTALLDIRS=vendor INSTALLMAN1DIR=none INSTALLMAN3DIR=none" --no-sign' + echo cpan2rpm --debug '--make-maker=--make-maker="INSTALLDIRS=vendor' INSTALLMAN1DIR=none 'INSTALLMAN3DIR=none"' --no-sign [...]
I get the same problem if I run bash from its /bin/sh symlink on RHEL6.
Any ideas what I'm missing here ?
* Which versions of bash and dash are you using on each host? * 'echo' will theoretically expand the quotes, and therefore you won't see how the arguments actually get passed to cpan2rpm. * Regarding one of the arguments themselves: --make-maker=--make-maker=... surely that's a typo? * I'm not sure of your overall goal, but based on what I've seen, I think you're going about this the wrong way: cpan2rpm() { command cpan2rpm --debug \ --make-maker=--make-maker="INSTALLDIRS=vendor INSTALLMAN1DIR=none \ INSTALLMAN3DIR=none" --no-sign $@; } * If you actually need to do it using a variable, one thought process I followed briefly was this, but it didn't work as the double-quotes didn't get expanded. I mention it only because somebody else might take it and run with it: mattcen@toto:tmp$ read -r a <<EOF > --debug --make-maker=--make-maker="INSTALLDIRS=vendor INSTALLMAN1DIR=none INSTALLMAN3DIR=none" --no-sign > EOF mattcen@toto:tmp$ echo $a --debug --make-maker=--make-maker="INSTALLDIRS=vendor INSTALLMAN1DIR=none INSTALLMAN3DIR=none" --no-sign * Alternatively, for something really ugly[1]: mattcen@toto:tmp$ parse-args() { while [ "$#" -gt 0 ]; do echo "$1"; shift; done; } mattcen@toto:tmp$ a='--debug@--make-maker=--make-maker="INSTALLDIRS=vendor INSTALLMAN1DIR=none INSTALLMAN3DIR=none"@--no-sign' mattcen@toto:tmp$ parse-args $a --debug@--make-maker=--make-maker="INSTALLDIRS=vendor INSTALLMAN1DIR=none INSTALLMAN3DIR=none"@--no-sign mattcen@toto:tmp$ export OIFS="$IFS" mattcen@toto:tmp$ export IFS=@ mattcen@toto:tmp$ parse-args $a --debug --make-maker=--make-maker="INSTALLDIRS=vendor INSTALLMAN1DIR=none INSTALLMAN3DIR=none" --no-sign mattcen@toto:tmp$ export IFS="$OIFS" -- Regards, Matthew Cengia