From owner-freebsd-questions@freebsd.org Mon Sep 21 05:12:27 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 2ADF53E5468 for ; Mon, 21 Sep 2020 05:12:27 +0000 (UTC) (envelope-from dpchrist@holgerdanske.com) Received: from holgerdanske.com (holgerdanske.com [184.105.128.27]) (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 "www.holgerdanske.com", Issuer "www.holgerdanske.com" (not verified)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Bvsy21hFjz3V0s for ; Mon, 21 Sep 2020 05:12:26 +0000 (UTC) (envelope-from dpchrist@holgerdanske.com) Received: from 99.100.19.101 (99-100-19-101.lightspeed.frokca.sbcglobal.net [99.100.19.101]) by holgerdanske.com with ESMTPSA (TLS_AES_128_GCM_SHA256:TLSv1.3:Kx=any:Au=any:Enc=AESGCM(128):Mac=AEAD) (SMTP-AUTH username dpchrist@holgerdanske.com, mechanism PLAIN) for ; Sun, 20 Sep 2020 22:12:24 -0700 Subject: Re: Error message output To: freebsd-questions@freebsd.org References: <20200920191108.22864e5c.freebsd@edvax.de> From: David Christensen Message-ID: <528b2c90-18c4-9e95-a150-67344154c66c@holgerdanske.com> Date: Sun, 20 Sep 2020 22:12:24 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.11.0 MIME-Version: 1.0 In-Reply-To: <20200920191108.22864e5c.freebsd@edvax.de> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Rspamd-Queue-Id: 4Bvsy21hFjz3V0s X-Spamd-Bar: +++ Authentication-Results: mx1.freebsd.org; dkim=none; dmarc=none; spf=none (mx1.freebsd.org: domain of dpchrist@holgerdanske.com has no SPF policy when checking 184.105.128.27) smtp.mailfrom=dpchrist@holgerdanske.com X-Spamd-Result: default: False [3.10 / 15.00]; RCVD_TLS_ALL(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; MID_RHS_MATCH_FROM(0.00)[]; FROM_HAS_DN(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; NEURAL_SPAM_SHORT(0.93)[0.929]; MIME_GOOD(-0.10)[text/plain]; TO_DN_NONE(0.00)[]; PREVIOUSLY_DELIVERED(0.00)[freebsd-questions@freebsd.org]; AUTH_NA(1.00)[]; RCPT_COUNT_ONE(0.00)[1]; NEURAL_SPAM_MEDIUM(0.54)[0.540]; ARC_NA(0.00)[]; NEURAL_SPAM_LONG(0.74)[0.736]; DMARC_NA(0.00)[holgerdanske.com]; R_SPF_NA(0.00)[no SPF record]; FROM_EQ_ENVFROM(0.00)[]; R_DKIM_NA(0.00)[]; MIME_TRACE(0.00)[0:+]; ASN(0.00)[asn:6939, ipnet:184.104.0.0/15, country:US]; 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 05:12:27 -0000 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? > > Example: > > if [ something not okay ]; then > echo "the error message" > /dev/stderr > exit 1 > fi > > While progress messages will per default go to standard output, > error messages should be printed to standard error. The reason: > If a program is silenced to > /dev/null, error messages will > still be visible (no "silent failing"); if a user wants to > explicitely mute all messages, > /dev/null 2>&1 has to be > specified for the redirection. The judgement if a message is > a regular progress message, an information about some slightly > problematic case, or a real fatal error depends on the programmer. I have been migrating my programming style towards a data flow paradigm, which includes "command-line filters". So, an "ideal" command-line program or script would: * Use stdin for the input data. * Use stdout for the output data. * Use configuration files, command-line options and arguments, received signals and direct tty reads for out-of-band/ non-data input. * Use stderr, log files, and the exit value for out-of-band/ non-data output. This model doesn't work for all programs, but it is nice when it does. A mouse and/or graphical environment adds even more possibilities. > For example: > > echo "${FILE] processed, ${RECS} records counted." > -> standard output If the above message represents the output data of the program, I would send it to stdout -- wc(1), for example. Otherwise, I would send it to stderr -- dd(1), for example. In the latter case, the message might be enabled or disabled by a configuration file setting and/or command-line option. > echo "${DIR} already checked, skipping." > -> standard output (non-fatal error" As above. > echo "${DEV} is read only, aborting." > exit 1 > -> standard error (fatal error) Yes, but don't you need to redirect echo(1) output to stderr? echo "writing to stderr" >&2 In some cases, it could be useful to print a warning to stderr and prompt the user to retry; again per configuration settings/ options. > echo "Cannot start: Input filename missing." > usage() > exit 1 > -> standard error (fatal error) As above. > At least that's what I've learned centuries ago. > > Is that still valid? As the author of a program, you decide what is valid. David