From nobody Tue Sep 28 11:59:42 2021 X-Original-To: freebsd-rc@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 193BC17E60C2; Tue, 28 Sep 2021 11:59:55 +0000 (UTC) (envelope-from SRS0=WfAV=OS=quip.cz=000.fbsd@elsa.codelab.cz) Received: from elsa.codelab.cz (elsa.codelab.cz [94.124.105.4]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 4HJdNV6RNQz4qTQ; Tue, 28 Sep 2021 11:59:54 +0000 (UTC) (envelope-from SRS0=WfAV=OS=quip.cz=000.fbsd@elsa.codelab.cz) Received: from elsa.codelab.cz (localhost [127.0.0.1]) by elsa.codelab.cz (Postfix) with ESMTP id 6427528417; Tue, 28 Sep 2021 13:59:47 +0200 (CEST) Received: from illbsd.quip.test (ip-78-45-215-131.net.upcbroadband.cz [78.45.215.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by elsa.codelab.cz (Postfix) with ESMTPSA id 6506828411; Tue, 28 Sep 2021 13:59:43 +0200 (CEST) Subject: Re: Quick fun question: only set an rc.d variable sometimes? To: "Dan Mahoney (Gushi)" , ports@freebsd.org References: <5dabceea-7f3c-efbe-3778-67ca360547a@prime.gushi.org> Cc: freebsd-rc@freebsd.org From: Miroslav Lachman <000.fbsd@quip.cz> Message-ID: <7d0ec8d5-1be3-dbd0-5f00-0cf51e3fa18f@quip.cz> Date: Tue, 28 Sep 2021 13:59:42 +0200 User-Agent: Mozilla/5.0 (X11; FreeBSD amd64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 List-Id: Discussion related to /etc/rc.d design and implementation List-Archive: https://lists.freebsd.org/archives/freebsd-rc List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-freebsd-rc@freebsd.org X-BeenThere: freebsd-rc@freebsd.org MIME-Version: 1.0 In-Reply-To: <5dabceea-7f3c-efbe-3778-67ca360547a@prime.gushi.org> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 4HJdNV6RNQz4qTQ X-Spamd-Bar: ---- Authentication-Results: mx1.freebsd.org; none X-Spamd-Result: default: False [-4.00 / 15.00]; REPLY(-4.00)[] X-ThisMailContainsUnwantedMimeParts: N 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