rc scripting trouble with quotes

Polytropon freebsd at edvax.de
Tue Jun 7 18:15:05 UTC 2016

On Tue, 7 Jun 2016 14:04:27 +0200, Murk Fletcher wrote:
> 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

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.

> 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).

> [...] 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)""

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. :-)

> 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

Magdeburg, Germany
Happy FreeBSD user since 4.0
Andra moi ennepe, Mousa, ...

More information about the freebsd-questions mailing list