X-Original-To: alpine-devel@lists.alpinelinux.org Received: from outgoing.fripost.org (giraff.fripost.org [178.16.208.44]) by lists.alpinelinux.org (Postfix) with ESMTP id D20C65C4A3D for ; Sat, 5 Nov 2016 19:50:44 +0000 (GMT) Received: from localhost (localhost [127.0.0.1]) by outgoing.fripost.org (Postfix) with ESMTP id 031FF8D5F87 for ; Sat, 5 Nov 2016 20:50:44 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=fripost.org; h= in-reply-to:content-disposition:content-type:content-type :mime-version:references:message-id:subject:subject:from:from :date:date; s=20140703; t=1478375443; x=1480189844; bh=pdSgRSurh g1P1n0Ati/JrjhgQXYm/ulzfmTyeb7tkgA=; b=USGi9Z1SK3X1gBo9NPa0Q07+t z8BTIs4c9TsqJOJrTCnQUbpdkD2KeTXR+SI7/n2kEdyXBOnPGuKy4RrqmX9LQPxt W9/Nx5llv2Y05/hp9Sp/24jaUCt7yAbYHy5mNIIt2BlseM1NCZIDotbQqvLAEPiI 16+ob0XinQfC0deqrY= X-Virus-Scanned: Debian amavisd-new at fripost.org Received: from outgoing.fripost.org ([127.0.0.1]) by localhost (giraff.fripost.org [127.0.0.1]) (amavisd-new, port 10040) with LMTP id UvzsHUVKTeCX for ; Sat, 5 Nov 2016 20:50:43 +0100 (CET) Received: from smtp.fripost.org (unknown [172.16.0.6]) by outgoing.fripost.org (Postfix) with ESMTP id DB5D28D5F84 for ; Sat, 5 Nov 2016 20:50:43 +0100 (CET) Received: from [127.0.0.1] (localhost [127.0.0.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by smtp.fripost.org (Postfix) with ESMTPSA id 136072599BE7 for ; Sat, 5 Nov 2016 20:50:37 +0100 (CET) Received: (qmail 26118 invoked from network); 5 Nov 2016 19:36:19 -0000 Received: from localhost (HELO aetey.se) (eh1ba719@127.0.0.1) by mail with ESMTPA; 5 Nov 2016 19:36:19 -0000 Date: Sat, 5 Nov 2016 20:50:23 +0100 From: u-ztsd@aetey.se To: Gemsbokella Cc: "alpine-devel@lists.alpinelinux.org" Subject: Re: [alpine-devel] Shell scripting guidelines Message-ID: <20161105195023.GX32190@example.net> References: <6i-dySQmcQujLFWVQ2tWvFh30Ag4r_F9P1_VIa0pNCHMaAvKMPx9XyoNjwLwzvsVU1riGlo2uzlvanNeeU07V0wPTAlPOdzuqEDLWhubKQE=@protonmail.ch> X-Mailinglist: alpine-devel Precedence: list List-Id: Alpine Development List-Unsubscribe: List-Post: List-Help: List-Subscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <6i-dySQmcQujLFWVQ2tWvFh30Ag4r_F9P1_VIa0pNCHMaAvKMPx9XyoNjwLwzvsVU1riGlo2uzlvanNeeU07V0wPTAlPOdzuqEDLWhubKQE=@protonmail.ch> 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 cut-n-paste. 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 (https://unix.stackexchange.com/questions/65803/why-is-printf-better-than-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. Regards, Rune --- Unsubscribe: alpine-devel+unsubscribe@lists.alpinelinux.org Help: alpine-devel+help@lists.alpinelinux.org ---