From owner-freebsd-questions@freebsd.org Mon Sep 21 22:56:06 2020 Return-Path: Delivered-To: freebsd-questions@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 21196421254 for ; Mon, 21 Sep 2020 22:56:06 +0000 (UTC) (envelope-from freebsd@edvax.de) Received: from mout.kundenserver.de (mout.kundenserver.de [217.72.192.75]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "mout.kundenserver.de", Issuer "TeleSec ServerPass Class 2 CA" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4BwKYD0QtGz45VQ for ; Mon, 21 Sep 2020 22:55:59 +0000 (UTC) (envelope-from freebsd@edvax.de) Received: from r56.edvax.de ([178.12.34.202]) by mrelayeu.kundenserver.de (mreue107 [212.227.15.183]) with ESMTPA (Nemesis) id 1MZk1p-1jzul527QV-00WnEo; Tue, 22 Sep 2020 00:55:52 +0200 Date: Tue, 22 Sep 2020 00:55:52 +0200 From: Polytropon To: David Christensen Cc: freebsd-questions@freebsd.org Subject: Re: Error message output Message-Id: <20200922005552.4df3c123.freebsd@edvax.de> In-Reply-To: <8b426d6f-6ebe-d1a7-13af-69cffbcb6222@holgerdanske.com> References: <20200920191108.22864e5c.freebsd@edvax.de> <528b2c90-18c4-9e95-a150-67344154c66c@holgerdanske.com> <20200921132139.286b5bda.freebsd@edvax.de> <8b426d6f-6ebe-d1a7-13af-69cffbcb6222@holgerdanske.com> Reply-To: Polytropon Organization: EDVAX X-Mailer: Sylpheed 3.1.1 (GTK+ 2.24.5; i386-portbld-freebsd8.2) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Provags-ID: V03:K1:LcnxzB9FvL32gWJwjEU3uHhSr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roSjJIV5MJAW/Y73iRyt9VcN5KTfNk98mBK+xsCkBU9LuRtduNeXkP3Ivm2BWaKQRlDM/Fp X-Rspamd-Queue-Id: 4BwKYD0QtGz45VQ X-Spamd-Bar: / Authentication-Results: mx1.freebsd.org; dkim=none; dmarc=none; spf=none (mx1.freebsd.org: domain of freebsd@edvax.de has no SPF policy when checking 217.72.192.75) smtp.mailfrom=freebsd@edvax.de X-Spamd-Result: default: False [0.95 / 15.00]; HAS_REPLYTO(0.00)[freebsd@edvax.de]; RCVD_VIA_SMTP_AUTH(0.00)[]; TO_DN_SOME(0.00)[]; MV_CASE(0.50)[]; HAS_ORG_HEADER(0.00)[]; RCPT_COUNT_TWO(0.00)[2]; RECEIVED_SPAMHAUS_PBL(0.00)[178.12.34.202:received]; RCVD_TLS_LAST(0.00)[]; R_DKIM_NA(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; ASN(0.00)[asn:8560, ipnet:217.72.192.0/20, country:DE]; ARC_NA(0.00)[]; NEURAL_HAM_MEDIUM(-0.84)[-0.840]; REPLYTO_EQ_FROM(0.00)[]; FROM_HAS_DN(0.00)[]; NEURAL_SPAM_SHORT(0.34)[0.343]; NEURAL_HAM_LONG(-0.95)[-0.952]; MIME_GOOD(-0.10)[text/plain]; DMARC_NA(0.00)[edvax.de]; AUTH_NA(1.00)[]; TO_MATCH_ENVRCPT_SOME(0.00)[]; MID_CONTAINS_FROM(1.00)[]; RCVD_IN_DNSWL_NONE(0.00)[217.72.192.75:from]; R_SPF_NA(0.00)[no SPF record]; RWL_MAILSPIKE_POSSIBLE(0.00)[217.72.192.75:from]; RCVD_COUNT_TWO(0.00)[2]; MAILMAN_DEST(0.00)[freebsd-questions] X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.33 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 21 Sep 2020 22:56:06 -0000 On Mon, 21 Sep 2020 15:33:40 -0700, David Christensen wrote: > On 2020-09-21 04:21, Polytropon wrote: > > On Sun, 20 Sep 2020 22:12:24 -0700, David Christensen wrote: > >> On 2020-09-20 10:11, Polytropon wrote: > >>> I have a general question. Is it still considered useful to > >>> output error messages of a script to standard error? > > > What about form? > > > > echo "the error message" > /dev/stderr > > > > or > > > > echo "the error message" >&2 > > > > WHich one is considered better form, leaving aside the "amount of > > symbols needed"? > > "It depends". The former is dependent upon the system having a > /dev/stderr. The latter is dependent upon the shell having redirection > (which all Bourne-compatible shells should?). Both seem to work on > FreeBSD, Debian, macOS, and Cygwin (Windows 7). But, that is likely to > be untrue on every system. And, is it possible to have a situation > where you have already redirect stderr and yet the script needs to emit > an error message? Without a compelling reason either way, I have tended > to use the latter; but the former is starting to look more idiot-proof. So sadly there no way of saying "one is to be preferred over the other". Sure, I agree that portability can be an issue even among UNIXes and UNIX-likes (like Linux), and I don't want to go that far and guess how sh script execution is becoming more and more possible under other systems and their layers or subsystems to make them able to do what everyone else can do for decades already. Coming from a heterogenous OS background, and especially one involving UNIXes from different ages, you would assume that "sh is sh", but it's not, and even if you think something is POSIX- compliant, it turns out it's not, at least not entirely, and that leads to all kinds of "careful coding". :-) The problem "script called from within script" adds an additional level of "didn't expect", such as ./something.sh > /dev/null 2>&1 where something.sh outputs stuff to stdout (like list of files processed) and stderr (specified files not found). In such cases, a log file which is handled independently from any redirection will surely be a better solution. Oh, and "idiot-proof" is always good, especially for idiot programmers like me. :-) > > I'm primarily interested in what currently the consensus is > > about good form and style, common approach and accepted ways > > of doing things. I could keep spamming the system log with > > progress messages, but that wouldn't be nice, would it? ;-) > > My ideal would be to put "less important" messages into a > program-specific log(s) and to put "important" messages into both > program-specific logs and the system log. Again, under configuration/ > option control. I'm primarily thinking about dead-simple idiot programs which are used interactively in order to perform some tasks, often involving dealing with files. Example (from a real script); the following normal usage prints to standard output: % png2pdf.sh mdcc_rg_2020-04-01 + mdcc_rg_2020-04-01_1.png + mdcc_rg_2020-04-01_2.png + mdcc_rg_2020-04-01_3.png -> mdcc_rg_2020-04-01.pdf The typical error cases (input not found, no input specified) output to standard error, and there is a non-zero exit code: % png2pdf.sh nothing Error: no matching source files for pattern nothing_*.png found, aborting. % png2pdf.sh Convert PNG image files and create PDF Usage: /opt/bin/png2pdf.sh The source file pattern must lead to files named _*.png. The target file's name will be .pdf. Is that an acceptable solution? > Truly sophisticated programs use a logging management layer. For > example, in Perl: > > https://metacpan.org/pod/Log::Log4perl No. Truly sophisticated programs use a web service with a dynamic no-SQL database and API and ML and AI provided by a startup with a name you cannot properly pronounce which will disappear in two years after you signed a 10 years contract and made your whoole business depend on it. ;-) -- Polytropon Magdeburg, Germany Happy FreeBSD user since 4.0 Andra moi ennepe, Mousa, ...