Date: Fri, 22 Apr 2011 01:28:40 +0200 From: Jilles Tjoelker <jilles@stack.nl> To: Doug Barton <dougb@FreeBSD.org> Cc: Garrett Cooper <yanegomi@gmail.com>, "freebsd-rc@FreeBSD.org" <freebsd-rc@freebsd.org>, "ggg_mail@inbox.ru" <ggg_mail@inbox.ru> Subject: Re: Finding $pidfile from a conf file (Was: Re: conf/153460: devd(8) cannot be restarted correctly via /etc/rc.d script) Message-ID: <20110421232840.GA29218@stack.nl> In-Reply-To: <4DACC455.4040603@FreeBSD.org> References: <201012272253.oBRMrH7k025331@freefall.freebsd.org> <1232C1B4-DA64-4332-906E-288B8C1E39FE@gmail.com> <4D1A3DFE.8090009@FreeBSD.org> <AANLkTinRSC_pBVdK_m8Hrk=_32mjMZDSc77__5iUyV_Z@mail.gmail.com> <4DACC455.4040603@FreeBSD.org>
next in thread | previous in thread | raw e-mail | index | archive | help
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
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20110421232840.GA29218>