bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Shell redirection success/failure isn't checked


From: Jeff Connelly
Subject: Re: Shell redirection success/failure isn't checked
Date: Sun, 29 Aug 2004 20:32:59 -0700

On Sun, 29 Aug 2004 17:00:13 -0400, Chet Ramey <chet.ramey@case.edu> wrote:
> Bash does, in fact, use its own echo builtin
> (try `type echo'), which returns failure when writes fail but does not
> print an error message.
> 
> The bash echo actually does the writes and calls ferror(), returning
> failure if ferror() returns non-zero.
<g> I used 'which' instead of 'type', my mistake. Attached is a patch to make
echo errors more descriptive. With the patch:

bash-3.00$ echo foo>bar
echo: write error: No space left on device
bash-3.00$ 

This is much more informative than simply setting $? to 1 for failure. MS Word
used to only have one error condition when failing to save to a document--Disk
is full--even when the disk wasn't full (see
http://blogs.msdn.com/rick_schaut/archive/2004/05/19/135315.aspx ) and this led
to serious problems and a hard-to-find bug. So I think
its good practice to at least print strerror(errno). The error
encountered by Microsoft
shouldn't happen at this level ("no file descriptors" will be caught
before the write), but
its possible, theoretically, that other write errors will occur.

I'd expect that most people, at least when using bash interactively,
do not check $?. Some explanation of the error could be useful. True,
some operating systems (FreeBSD) report "/filesystem: write failed,
filesystem full" in the kernel output buffer, but not all. Linux in
particular doesn't give any warning for some reason (I've been told
its the same reason there is no "undelete" command, perhaps its a
technical reason), leaving the error reporting at the mercy of the
userland.

_The Art of Unix Programming_
(http://www.faqs.org/docs/artu/ch01s06.html#id2878538 ) has this
guideline:
"when you must fail, fail noisily and as soon as possible" and I think
it is appropriate in this situation.

Best regards,
-Jeff Connelly

--- builtins/echo.def.orig      Sun Aug 29 14:07:37 2004
+++ builtins/echo.def   Sun Aug 29 14:16:45 2004
@@ -29,6 +29,7 @@
 #include "../bashansi.h"

 #include <stdio.h>
+#include <errno.h>
 #include "../shell.h"

 $BUILTIN echo
@@ -171,6 +172,7 @@
   if (ferror (stdout))
     {
       clearerr (stdout);
+      fprintf (stderr, "%s: write error: %s\n", "echo", strerror(errno));
       return (EXECUTION_FAILURE);
     }
   return (EXECUTION_SUCCESS);
0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]