tech-pkg archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: rc.d script style guide



    Date:        Sat, 07 Jan 2023 08:52:15 -0500
    From:        Greg Troxel <gdt%lexort.com@localhost>
    Message-ID:  <rmik01yv4ds.fsf%s1.lexort.com@localhost>

  | 1) conditional /etc/rc.subr

I'm not going to touch what pkgsrc should do with rc.d scripts, nor
what systems they should be able to run on (which I'd guess would depend
upon whether the package normally runs elsewhere, using other mechanisms,
and the rc.d script is just intended to be used on systems where it is
needed, or if the package originates on an rc.d using system, and the
script is its sole way to be started, even on systems that don't use rc 
scripts normally).

  |   if [ -f /etc/rc.subr -a -f /etc/rc.conf -a -d /etc/rc.d -a -f /etc/rc.d/DAEMON ]
  |   then

  | 2) Strange formatting with then on a new line (see above)

What's supposedly strange about that?   That's how I'd usually write
a sh if statement (unless the code is very small, and the whole thing
will fit on one line).

  | Is this:
  |   - just an odd style choice?

Depends upon your definition of odd.   In this case, the if [...]
ends up being (about, if not exactly) 80 chars wide (with the 'i' from 'if'
in column 1) so adding more to the end of the line would mean line
wrapping anyway - in that case, I'd *always* (even if other line wrapping
is also needed) put the "then" on a line by itself.

  |   - accomodating some ancient sh?

No.

  |   - for a good reason I don't understand?

Reserved words in sh (of which "then" is one) must occur in a "command
word" position, which (roughly) means after a ';' '|' '&' or newline (there
are more, including after another reserved word, when the syntax permits).

So, the alternatives there (for that usage) are

	if [ whatever ]; then
or
	if [ whatever ]
	then

and of those, personally, I prefer the 2nd most of the time, it keeps
the "if" related syntax all lined up, rather than spread out.

What ought to be changed in that is the use of -a in the test ('[') command.
That's obsolete (because it can be ambiguous, and cause problems), so,
assuming that all those tests are necessary (no opinion on that), I'd
rewrite it as

if [ -f /etc/rc.subr ] && [ -f /etc/rc.conf ] && [ -d /etc/rc.d ] &&
   [ -f /etc/rc.d/DAEMON ]
then
	load_rc_config "$name"
	run_rc_command "$1"
else
	eval ${start_cmd}
fi

You could even write it:

if [ -f /etc/rc.subr ] &&
   [ -f /etc/rc.conf ] &&
   [ -d /etc/rc.d ]    &&
   [ -f /etc/rc.d/DAEMON ]
then
	load_rc_config "$name"
	run_rc_command "$1"
else
	eval ${start_cmd}
fi

of indent the '[' with a tab (all uses) so it lines up with the commands
below).   Many possible style choices.  Most sh scripts (and particularly
rc.d scripts) are quite short, so the need for a consistent style, to make
comprehension easier, isn't nearly as great as with typical C programs.

Oh, and I know I said I wouldn't comment, but the 3rd of those tests (the -d
test) is certainly not needed, if that fails, then the 4th would (if run)
certainly fail as well, if it succeeds, we need to run the 4th anyway, so
that -d test adds nothing even possibly useful.   Remove that one, and the
need to continue the if condition onto a 2nd line goes away.

kre

ps: don't copy style from one language to another.  In C, I would always
(or almost always) put the '{' that begins a block of code on the same line
as the end of the "if" condition - but C isn't sh, C conditions are 
expressions, sh conditions are statements.



Home | Main Index | Thread Index | Old Index