I have a script that accepts parameters that I'd like to be able to run as a linux service. There is a symlink to it from /etc/ini.t/deploy to the actual script.
If I run
/etc/init.d/deploy --machine NAME --message "Hello World"
everything works find and the script receives 4 arguments:
'--machine',
'NAME',
'--message',
'Hello World'
On the other hand, if I run it as
/sbin/service deploy --machine NAME --message "Hello World"
then the script receives 5 parameters:
'--machine',
'NAME',
'--message',
'Hello',
'World'
The same happens both in Perl and Python, so as I understand it is the "service" that passes the arguments after splitting up on space. What can I do to make this work the same way with "service" as when calling directly?
That's the problem of passing arguments. Obviously something is not passed verbatim in your context but interpretingly, so to speak.
You can use the workaround of quoting your stuff twice:
/sbin/service deploy --machine NAME --message '"Hello World"'
In many cases this solves the issue.
A more general approach is to use the printf "%q" of modern bashs:
/sbin/service deploy --machine NAME --message "$(printf "%q" "Hello World")"
This generates a quoted version of the given string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With