Re: Quick fun question: only set an rc.d variable sometimes?

From: Dan Mahoney <freebsd_at_gushi.org>
Date: Tue, 28 Sep 2021 18:07:57 UTC
Miroslav,

Thanks much for this!

Some of this would be useful additions/further-reading to the handbook page I originally quoted.  I'm happy to prepare a diff, if there's reasonable chance it would be accepted.

-Dan

> On Sep 28, 2021, at 4:59 AM, Miroslav Lachman <000.fbsd@quip.cz> wrote:
> 
> On 28/09/2021 05:23, Dan Mahoney (Gushi) wrote:
>> Hey all,
>> I'm dealing with rc.d scripting and reading https://docs.freebsd.org/en/articles/rc-scripting/
>> Here's my question: Is there a sane way to have something like foo_pid *completely unset* in one case, but overridable by rc.conf after?
>> It took me a bit to wrap my head around the:
>> : ${dummy_enable:=no}
>> : ${dummy_msg="Nothing started."}
>> Examples.  (Why that first colon, what is this := syntax), etc.
> 
> The first colon is builtin function same as /bin/true
> 
> A useful application for : is if you're only interested in using parameter expansions for their side-effects rather than actually passing their result to a command.
> You can use the parameter expansion as an argument to :
> 
> : "${var:=$1}"
> 
> There is as many variants to handle this as many rc scripts are installed on your system. Each have it slightly different (syntax) but with the same meaning:
> 
> Apache
> [ -z "$apache24_enable" ]       && apache24_enable="NO"
> [ -z "$apache24limits_enable" ] && apache24limits_enable="NO"
> [ -z "$apache24limits_args" ]   && apache24limits_args="-e -C daemon"
> [ -z "$apache24_http_accept_enable" ] && apache24_http_accept_enable="NO"
> [ -z "$apache24_configcheck_disable" ] && apache24_configcheck_disable="NO"
> 
> Amavisd
> : ${amavisd_enable:=NO}
> pidfile=${amavisd_pidfile-"/var/amavis/amavisd.pid"}
> 
> ISC DHCPD
> # default name to "dhcpd" if guessing failed
> # Trailing semicolon also for service(8)'s benefit:
> name="${name:-dhcpd}" ;
> name=${name##*/isc-}
> 
> 
> Some more explanation:
> 
> To get the assigned value, or default if it's missing:
> 
> FOO="${VARIABLE:-default}"
> 
> Or to assign default to VARIABLE at the same time:
> 
> FOO="${VARIABLE:=default}"
> 
> Or check the link https://bash.cyberciti.biz/guide/Default_shell_variables_value
> 
>> What I'm trying to say in rc.subr language is:
>> If the user has set something in rc.conf, use it.  Otherwise, leave it TOTALLY UNSET.  Not to the null string.  Undefined.
> 
> Easily readable syntax can be like this:
> 
> [ -n "$kdc_pid" ] && pidfile="$kdc_pid"
> 
> Variable pidfile will be set to a value of $kdc_pid only if $kdc_pid is set and is not empty.
> 
> Miroslav Lachman