[GRASS-dev] Merging stdout and stderr

Glynn Clements glynn at gclements.plus.com
Tue Nov 21 14:36:10 EST 2006


Hamish wrote:

> > >>> 2. Might it be a worthwhile idea to include a simple program with
> > >>> GRASS that did this (copies everything from stdin to stdout) so we
> > >>> can be sure what's going on?
> > >>
> > >> I suggest the latter, i.e. write our own mini-"cat" program. It
> > >> will need to explicitly put stdin/stdout into line-buffered mode
> > >> (pipes are block-buffered by default).
> 
> Hamish:
> > > see also:
> > >  lib/init/echo.c -> $GISBASE/etc/echo
> > >
> > > funny that that outputs to stderr..
> 
> Paul:
> > Yes! This would be useful on Windows if it didn't write to stderr, as
> > the Windows echo behaves differently to the Unix one. The comments in
> > the header in the source say it writes to stdout but the earliest CVS
> > version from 1999 writes to stderr. I wonder could we change it. I
> > wonder does  anything use it and/or need the stderr functionality...
> 
> 
> grass63$ grep -rI etc/echo *
> lib/init/echo.c: * $GISBASE/etc/echo [-n] args
> 
> grass-5.4.0$ grep -rI etc/echo *
> pkg/prototype:f none /opt/GNUgrass/grass5/etc/echo=/opt/GNUgrass/grass5/etc/echo 0775 root bin
> rpmspecs/rpm.specs:%{_prefix}/grass5/etc/echo
> src/general/init/echo.c: * $GISBASE/etc/echo [-n] args
> src.contrib/SCS/scripts/shells/dlg_extract.sh:$GISBASE/etc/echo -n  "Enter DEVICE or FILE_NAME for input : "
> src.contrib/SCS/scripts/shells/dlg_extract.sh:$GISBASE/etc/echo -n  "Enter TOTAL NUMBER of FILES : "
> src.contrib/SCS/scripts/shells/dlg_extract.sh:$GISBASE/etc/echo -n  "Enter LRECL: "
> src.contrib/SCS/scripts/shells/dlg_extract.sh:$GISBASE/etc/echo -n  "Enter BLKSIZE:  "
> src.contrib/SCS/scripts/shells/dlg_extract.sh:  $GISBASE/etc/echo -n  "Enter NUMBER OF FILE TO EXTRACT: "
> src.contrib/SCS/scripts/shells/dlg_extract.sh:  $GISBASE/etc/echo -n  "Enter No. of Records: "
> src.contrib/SCS/scripts/shells/dlg_extract.sh:  $GISBASE/etc/echo -n  "Enter name for output file: "
> src.contrib/SCS/scripts/shells/dlg_extract.sh:  $GISBASE/etc/echo -n  "ARE THESE CORRECT [y/n]: "
> src.contrib/SCS/scripts/shells/dlg_extract.sh:  $GISBASE/etc/echo -n  "Do you want to EXTRACT another FILE [y/n]: "

That explains a lot about the fact that it writes to stderr, i.e. the
fact that it is being used to display prompts (which should normally
go to stderr), and the fact that the output doesn't normally have a
newline (the program doesn't call fflush(), so the output wouldn't
appear if it went to stdout).

It looks like it was intended as a portable way to use "echo -n ...". 
The SUSv2 specification for echo doesn't support any flags; instead,
you need to put '\c' at the end of the string to suppress the newline. 
OTOH, the GNU version and the bash built-in don't support the \c
escape.

-- 
Glynn Clements <glynn at gclements.plus.com>




More information about the grass-dev mailing list