Mail archive

Re: [alpine-devel] Shell scripting guidelines

From: <>
Date: Sat, 5 Nov 2016 20:50:23 +0100

Disclaimer: I am not involved in Alpine-specific scripting at all.

Nevertheless I have a lot to do with shell scripting and came with time
to some conclusions. They are of course influenced by a personal taste
but mostly reflect the experience, through generations of shells.

Here follows my 2 cents:

I find all the cited recommendations reasonable, besides a single one.

On Sat, Nov 05, 2016 at 03:06:28PM -0400, Gemsbokella wrote:
> ===Style===
> * Use tabs for indentation

Sorry, my stance is "tabs are harmful":

 - they are invisible so you never know without extra tricks
   whether and how many spaces/tabs are there
 - they are interpreted differently by different editors and
   in different situations
 - they can not be reliably cut&pasted (because they are
   often visualised via some spaces)
 - they do not provide reliable indentation (because of varying
   interpretation by viewers/editors)
 - the classical interpretaion of 8 positions gives inefficient
   (and inflexible) large indentation, easily leading to long lines
   or/and line wrapping

   Depending on the script contents, a smaller indentation
   (say, 4 or sometimes even 2 positions) can remain easy to read and
   at the same time prevent moving the indented lines out of sight.

In other words tabs are PITA. Thanks for listening.

> Question 1: User input is very hard to get right, especially in shell scripts. How to deal with invalid input? Ask again? Exit?
> Opinion 1: I personally ask the user again. Another common practise is just to quit the script which, frankly, is not a very good solution.

Depends on the situation. IMHO dangerous operations should become canceled
if the user can not spell the answer, otherwise you expose your script
among others to the user-in-a-hurry-typeahead or unintended multiline

Otherwise to give the user a new chance after a typo is of course nice
and useful.

> Question 3: printf vs echo
> Opinion 3: 99% of the time echo ( I think we should accept this as a rule ASAP.

Echo is just a very old artefact, it exists historically but should
be never used, due to its various and uncorrectable flaws.

OTOH as long as there is a common library which e.g. defines echo
via printf, making it deterministic, then it is as good as any
other in-house shell function, if treated as such. I.e. you do not
have to clean away all "echo"s from the scripts, if the scripts
happen to rely on a function library.

Hope these notes help somehow.


Received on Sat Nov 05 2016 - 20:50:23 GMT