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

Re: Using /dev/stderr vs. >&2



On Wed, Jul 01, 2009 at 10:50:52AM +0200, Sven Joachim wrote:
> On 2009-07-01 10:14 +0200, Todd A. Jacobs wrote:
> 
> > I noticed that a bash script of mine was causing permission errors under
> > cron, so I had to change it like so:
> >
> >     --- boxmail.sh  2009/06/30 09:01:46     1.10
> >     +++ boxmail.sh  2009/07/01 08:01:51
> >     @@ -167,7 +167,7 @@
> >
> >      # Echo message to standard error.
> >      function stderr {
> >     -    echo "$*" > /dev/stderr
> >     +    echo "$*" >&2
> >      }
> >
> >      # Display warnings on standard error.
> >
> > I did a little minor testing with cron, and >&2 works while /dev/stderr
> > doesn't. Now, I know that "/dev/stderr" is a bashism,
> 
> Actually, it might be not.  On my system, /dev/stderr exists and is a
> symlink to /proc/self/fd/2, i.e. the standard error channel of the
> current process.  Thus it works fine with dash.

Right. On linux:

$ ls -l /dev/stderr
lrwxrwxrwx 1 root root 15 2009-06-10 23:35 /dev/stderr -> /proc/self/fd/2

On BSDs and Solaris /dev/stderr is a special device (like /dev/null and
/dev/zero). Though I'm not really sure it is guaranteed by POSIX.

> 
> > but since cron is
> > executing boxmail.sh (with an explicit bash shebang) rather than
> > executing the code directly, why would the bashism come back with:
> >
> >     From: Cron Daemon
> >     To: nospam
> >     Subject: Cron <nospam> $HOME/bin/boxmail.sh
> >     Date: Wed,  1 Jul 2009 00:00:02 -0700 (PDT)
> >
> >     /home/nospam/bin/boxmail.sh: line 170: /dev/stderr: Permission denied

ls -l /dev/stderr

> >
> > The bash manual doesn't say anything about a requirement that the shell
> > be interactive to use /dev/stderr, so why is this happening?

A redirection to &2 is cleaner, anyway. Any reason you have not to use
it?

-- 
Tzafrir Cohen         | tzafrir@jabber.org | VIM is
http://tzafrir.org.il |                    | a Mutt's
tzafrir@cohens.org.il |                    |  best
ICQ# 16849754         |                    | friend


Reply to: