Finding $pidfile from a conf file (Was: Re: conf/153460: devd(8)
cannot be restarted correctly via /etc/rc.d script)
Jilles Tjoelker
jilles at stack.nl
Thu Apr 21 23:28:42 UTC 2011
On Mon, Apr 18, 2011 at 04:08:05PM -0700, Doug Barton wrote:
> #!/bin/sh
>
> # get_pidfile_from_conf string file
> #
> # Takes a string to search for in the specified file.
> # Ignores lines with traditional comment characters.
> #
> # Example:
> # if ! pidfile=`get_pidfile_from_conf "string" "file" 2>/dev/null`; then
> # pidfile='/path/to/default'
> #fi
> #
> get_pidfile_from_conf()
> {
> local string file line
>
> string="$1" ; file="$2"
>
> if [ -z "$string" -o -z "$file" ] || [ ! -s "$file" ]; then
> err 3 'USAGE: get_pidfile_from_conf string file'
> fi
>
> while read line; do
> case "$line" in
> *[#\;]*${string}*) continue ;;
> *${string}*) break ;;
> esac
> done < $file
>
> if [ -n "$line" ]; then
> line=${line#*/}
> line="/${line%%[\"\;]*}"
The meaning of this line depends on the version of sh(1). The correct
interpretation, used by sh in 9-current and most other shells, is to
strip from the first double-quote or semicolon onwards. However, sh in
older FreeBSD versions will strip from the first backslash, double-quote
or semicolon onwards.
If the 9-current behaviour is desired for all FreeBSD versions, use:
line=/${line%%[\"\;]*}
Perhaps spaces should be treated as a field terminator as well.
> echo $line
> else
> return 1
> fi
> }
>
> if ! pidfile=`get_pidfile_from_conf "$1" "$2" 2>/dev/null`; then
This kind of construction invokes the function in a subshell, which is
less efficient. Passing the name of a variable to write the result into
is more efficient.
It is advisable to set up the init script such that work like
get_pidfile_from_conf is only done if the service is actually enabled.
> pidfile='some default'
> fi
> echo "pidfile: $pidfile"
> exit $?
--
Jilles Tjoelker
More information about the freebsd-rc
mailing list