Discussion:
rc scripting trouble with quotes
(too old to reply)
Murk Fletcher
2016-06-07 12:04:27 UTC
Permalink
Hi!

How do I access a variable inside quotes? Right now I'm having some
difficulties:

stop_cmd="cd ${myapp} && \
${myapp} stop && \
kill -9 `cat ${myapp}/tmp/pids/example.pid`"

Returns:

cat: ${myapp}/tmp/pids/example.pid: No such file or directory

I hear it would work better with double quotes, but that would add a
double-double quote at the end:

stop_cmd="cd ${myapp} && \
${myapp} stop && \
kill -9 "$(cat -- ${myapp}/tmp/pids/example.pid)""

Is there a way I could wrap the contents of `stop_cmd` inside a function or
something?

Thanks!

--Murk

https://freebsd.org/doc/en/articles/rc-scripting/rcng-dummy.html
Murk Fletcher
2016-06-07 13:49:01 UTC
Permalink
For what it's worth I ended up with the whole function thing and it's
working perfectly:

dummy_stop() {
cd ${myapp} &&
${myapp} stop &&
kill -9 "$(cat -- ${myapp}/tmp/pids/example.pid)"
}

stop_cmd="dummy_stop"

Thanks!

--Murk
Post by Murk Fletcher
Hi!
How do I access a variable inside quotes? Right now I'm having some
stop_cmd="cd ${myapp} && \
${myapp} stop && \
kill -9 `cat ${myapp}/tmp/pids/example.pid`"
cat: ${myapp}/tmp/pids/example.pid: No such file or directory
I hear it would work better with double quotes, but that would add a
stop_cmd="cd ${myapp} && \
${myapp} stop && \
kill -9 "$(cat -- ${myapp}/tmp/pids/example.pid)""
Is there a way I could wrap the contents of `stop_cmd` inside a function
or something?
Thanks!
--Murk
https://freebsd.org/doc/en/articles/rc-scripting/rcng-dummy.html
RW via freebsd-questions
2016-06-07 17:20:01 UTC
Permalink
On Tue, 7 Jun 2016 14:04:27 +0200
Post by Murk Fletcher
Hi!
How do I access a variable inside quotes? Right now I'm having some
stop_cmd="cd ${myapp} && \
${myapp} stop && \
kill -9 `cat ${myapp}/tmp/pids/example.pid`"
Are you sure you really need to do this? Stopping daemons is a basic
part of what rc.subr does.
Polytropon
2016-06-07 18:06:05 UTC
Permalink
Post by Murk Fletcher
Hi!
How do I access a variable inside quotes? Right now I'm having some
stop_cmd="cd ${myapp} && \
${myapp} stop && \
kill -9 `cat ${myapp}/tmp/pids/example.pid`"
cat: ${myapp}/tmp/pids/example.pid: No such file or directory
Have you checked the actual value of that expression? For testing,
insert something like

echo ${myapp}/tmp/pids/example.pid

to see if you _really_ get the file name you're expecting.
Post by Murk Fletcher
I hear it would work better with double quotes, [...]
There is no "work better" here: If you use double quotes, variables
will be expanded; if you use single quotes, they will not. Double
quotes are usually needed when a path contains spaces (because the
space character is the argument separator, while it also is a valid
character for file names).
Post by Murk Fletcher
[...] but that would add a
stop_cmd="cd ${myapp} && \
${myapp} stop && \
kill -9 "$(cat -- ${myapp}/tmp/pids/example.pid)""
You could use qupting, \", but that probably won't work as intended.
You need to use quotes because the value you assign to $stop_cmd
contains spaces. You could use quoted spaces, \ , to get rid of
them, but that makes the whole thing nearly unreadable and will
probably introduce more problems. :-)
Post by Murk Fletcher
Is there a way I could wrap the contents of `stop_cmd` inside a function or
something?
Basically, your initial assignment looks correct, there surely
is a different problem (maybe with the evaluation of ${myapp}.

As it as already been mentioned, /etc/rc.subr should do what's
needed to stop a program, maybe you don't even need to do this
manually.
--
Polytropon
Magdeburg, Germany
Happy FreeBSD user since 4.0
Andra moi ennepe, Mousa, ...
Loading...