Coming Underground
« USB tethering with Cyanogenmod and GentooRecent udev and legacy Xen on Gentoo »

Having fun with start-stop-daemon and quoting

Permalink 2009-12-18 08:37, by jaervosz, Categories: General, Gentoo Tips, Universe/English

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

Leave a comment »Send a trackback »

Trackback address for this post

This is a captcha-picture. It is used to prevent mass-access by robots.
Please enter the characters from the image above. (case insensitive)

No feedback yet

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)
This is a captcha-picture. It is used to prevent mass-access by robots.
Please enter the characters from the image above. (case insensitive)

©2010 by admin

Contact Jaervosz