| « USB tethering with Cyanogenmod and Gentoo | Recent udev and legacy Xen on Gentoo » |
Having fun with start-stop-daemon and quoting
Yesterday night I had the opportunity to have a small dance with the start-stop-daemon, I don't dance very often and certainly not with the start-stop-daemon, so the result was not very pretty.
Perhaps I just needed more caffeine, perhaps I just don't understand start-stop-daemon quotation (I don't).
My problem is similar to this one, but I'm too stubborn to accept those workarounds.
Anyways I tried to simplify my problem into a simple example; how to use a Bash variable with spaces and get start-stop-daemon quotation right.
First simple attempt:
mkdir /tmp/path\ with\ spaces
cat test.sh
#!/bin/bash -x
opt="-lah '/tmp/path with spaces/'"
start-stop-daemon --start --exec /usr/bin/ls -- ${opt}
./test.sh
+ opt='-lah '\''/tmp/path with spaces/'\'''
+ start-stop-daemon --start --exec /usr/bin/ls -- -lah ''\''/tmp/path' with 'spaces/'\'''
/usr/bin/ls: cannot access '/tmp/path: No such file or directory
/usr/bin/ls: cannot access with: No such file or directory
/usr/bin/ls: cannot access spaces/': No such file or directory
As you can see naively quotation is not as expected when the command gets executed by the start-stop-daemon.
Let's try another one:
cat test1.sh
#!/bin/bash -x
opt="-lah \"/tmp/path with spaces/\""
start-stop-daemon --start --exec /usr/bin/ls -- ${opt}
./test1.sh
+ opt='-lah "/tmp/path with spaces/"'
+ start-stop-daemon --start --exec /usr/bin/ls -- -lah '"/tmp/path' with 'spaces/"'
/usr/bin/ls: cannot access "/tmp/path: No such file or directory
/usr/bin/ls: cannot access with: No such file or directory
/usr/bin/ls: cannot access spaces/": No such file or directory
Still not working. Grrr, awful, hairpulling. BAH! It was about to really ruin my evening (when I get really stubborn I don't eat or drink until the problem is solved).
Thanks to Roy Marples here is a possible solution. Split out the options from the path and use eval:
cat test2.sh
#!/bin/bash -x
path="/tmp/path with spaces/"
opt="-lah"
eval start-stop-daemon --start --exec /usr/bin/ls -- "$opt" \"$path\"
./test2.sh
+ path='/tmp/path with spaces/'
+ opt=-lah
+ eval start-stop-daemon --start --exec /usr/bin/ls -- -lah '"/tmp/path' with 'spaces/"'
++ start-stop-daemon --start --exec /usr/bin/ls -- -lah '/tmp/path with spaces/'
total 8.0K