Date: Sun, 20 Sep 2020 22:12:24 -0700 From: David Christensen <dpchrist@holgerdanske.com> To: freebsd-questions@freebsd.org Subject: Re: Error message output Message-ID: <528b2c90-18c4-9e95-a150-67344154c66c@holgerdanske.com> In-Reply-To: <20200920191108.22864e5c.freebsd@edvax.de> References: <20200920191108.22864e5c.freebsd@edvax.de>
next in thread | previous in thread | raw e-mail | index | archive | help
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
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?528b2c90-18c4-9e95-a150-67344154c66c>