Usenet comp.unix.osf.osf1 (1992-2004)
 help / color / mirror / Atom feed
* no /dev/stderr ?
@ 2000-02-23  5:00 Usman Muzaffar
  2000-02-24  5:00 ` Bob Harris
  0 siblings, 1 reply; 4+ messages in thread
From: Usman Muzaffar @ 2000-02-23  5:00 UTC (permalink / raw)



Hi -

Tru64 4.0 - I can't see a /dev/stderr in the device
hierarchy. On other unices (linux, solaris) it's usually
a symlink to the device that corresponds to fd 2.
Can I create it myself?

Isn't this POSIXish? Say I have a Bourne shell script
whose output I want to direct to stderr or a file or
/dev/null, depending on a switch. How can I do it if
there's no way to reference /dev/stderr ?

thanks,
usman
remove x from above to reply, if present



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: no /dev/stderr ?
  2000-02-23  5:00 no /dev/stderr ? Usman Muzaffar
@ 2000-02-24  5:00 ` Bob Harris
  2000-02-25  5:00   ` Timo Felbinger
  0 siblings, 1 reply; 4+ messages in thread
From: Bob Harris @ 2000-02-24  5:00 UTC (permalink / raw)
  To: Usman Muzaffar

[[ This message was both posted and mailed: see
   the "To," "Cc," and "Newsgroups" headers for details. ]]

In article <38B48886.6AB7691C@thynne.psych.nwux.edu>, Usman Muzaffar
<usman@thynne.psych.nwux.edu> wrote:
>Tru64 4.0 - I can't see a /dev/stderr in the device
>hierarchy. On other unices (linux, solaris) it's usually
>a symlink to the device that corresponds to fd 2.
>Can I create it myself?
>
>Isn't this POSIXish? Say I have a Bourne shell script
>whose output I want to direct to stderr or a file or
>/dev/null, depending on a switch. How can I do it if
>there's no way to reference /dev/stderr ?

In the Bourne or Korn shells if you want to redirect I/O to stderr, you
do the following:

    command args 1>&2

if you want to direct both stderr and stdout into a file you do the
following in the following order:

    command args >file 2>&1

/dev/stderr is _NOT_ a standard.  /dev/console, /dev/null, and /dev/tty
are standards.  Those are the only 3 /dev/ devices that I can find in
the standard.  You can read the Open Group X/Open standards on-line
with a free registration at:

    <http://www.opengroup.org/online-pubs?DOC=007908799>



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: no /dev/stderr ?
  2000-02-24  5:00 ` Bob Harris
@ 2000-02-25  5:00   ` Timo Felbinger
  2000-02-25  5:00     ` Bob Harris
  0 siblings, 1 reply; 4+ messages in thread
From: Timo Felbinger @ 2000-02-25  5:00 UTC (permalink / raw)




On Thu, 24 Feb 2000, Bob Harris wrote:

> [[ This message was both posted and mailed: see
>    the "To," "Cc," and "Newsgroups" headers for details. ]]
> 
> In article <38B48886.6AB7691C@thynne.psych.nwux.edu>, Usman Muzaffar
> <usman@thynne.psych.nwux.edu> wrote:
> >Tru64 4.0 - I can't see a /dev/stderr in the device
> >hierarchy. On other unices (linux, solaris) it's usually
> >a symlink to the device that corresponds to fd 2.
> >Can I create it myself?
> >
> >Isn't this POSIXish? Say I have a Bourne shell script
> >whose output I want to direct to stderr or a file or
> >/dev/null, depending on a switch. How can I do it if
> >there's no way to reference /dev/stderr ?
> 
> In the Bourne or Korn shells if you want to redirect I/O to stderr, you
> do the following:
> 
>     command args 1>&2
> 

This was my first idea, too, but it won't help if you want to do
something like

  command args 2> $logfile

where $logfile is either /dev/null or stderr: > and >& are different
redirection operators, and unfortunately, they can't be the result
of a variable expansion. /dev/stderr is there for a reason.

Some (clumsy) workarounds:
  - use
      lsof -p <PID> -a -d 2
    to determine what descriptor 2 is actually connected to,
  - or use a pipeline like
      {
        command1
        command2
        .
        .
        .
      } 2>&1 | {
        case $logfile in
          stderr )
            cat >& 2
          ;;
          * )
            cat > $logfile
          ;;
        esac
      }

Regards,

Timo Felbinger








^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: no /dev/stderr ?
  2000-02-25  5:00   ` Timo Felbinger
@ 2000-02-25  5:00     ` Bob Harris
  0 siblings, 0 replies; 4+ messages in thread
From: Bob Harris @ 2000-02-25  5:00 UTC (permalink / raw)
  To: usman

[[ This message was both posted and mailed: see
   the "To," "Cc," and "Newsgroups" headers for details. ]]

In article
<Pine.LNX.4.21.0002251011230.6234-100000@uranos.quantum.physik.uni-potsd
am.de>, Timo Felbinger <timof@uranos.quantum.physik.uni-potsdam.de>
wrote:
 >On Thu, 24 Feb 2000, Bob Harris wrote:
 >> [[ This message was both posted and mailed: see
 >>    the "To," "Cc," and "Newsgroups" headers for details. ]]
 >> In article <38B48886.6AB7691C@thynne.psych.nwux.edu>, Usman Muzaffar
 >> <usman@thynne.psych.nwux.edu> wrote:
 >>>Tru64 4.0 - I can't see a /dev/stderr in the device
 >>>hierarchy. On other unices (linux, solaris) it's usually
 >>>a symlink to the device that corresponds to fd 2.
 >>>Can I create it myself?
 >>>
 >>>Isn't this POSIXish? Say I have a Bourne shell script
 >>>whose output I want to direct to stderr or a file or
 >>>/dev/null, depending on a switch. How can I do it if
 >>>there's no way to reference /dev/stderr ?
 >> 
 >> In the Bourne or Korn shells if you want to redirect I/O to stderr,
 >> you do the following:
 >> 
 >>     command args 1>&2
 >> 
 >This was my first idea, too, but it won't help if you want to do
 >something like
 >
 >  command args 2> $logfile
 >
 >where $logfile is either /dev/null or stderr: > and >& are different
>redirection operators, and unfortunately, they can't be the result
 >of a variable expansion. /dev/stderr is there for a reason.
 >
 >Some (clumsy) workarounds:
 >  - use
 >      lsof -p <PID> -a -d 2
 >    to determine what descriptor 2 is actually connected to,
 >  - or use a pipeline like
 >      {
 >        command1
 >        command2
 >        ...
 >      } 2>&1 | {
 >        case $logfile in
 >          stderr )
 >            cat >& 2
 >          ;;
 >          * )
 >            cat > $logfile
 >          ;;
 >        esac
 >      }
                                                              
I didn't say /dev/stdout wasn't a good idea, only that it was not a
standard, which means it is not portable.

If you use the 'eval' command you can get what you want.  I also know
that this is not a straight forward, nor as obvious to the person
reading the script as using a /dev/stderr, but it will work.  For
example:

#-----------------------------------------------------------------------
#!/bin/ksh
STDERR="2>file.stderr"
STDOUT=">file.stdout"
write_stderr()
{
        echo "this is something written to stderr" 1>&2
}
write_stdout()
{
        echo "this is something written to stdout"
}
eval write_stderr $STDERR
eval write_stdout $STDOUT
exit
#-----------------------------------------------------------------------

The above script will be portable, and it will allow the redirection of
stderr and stdout to be placed in a variable that may be changed by the
script depending on conditions.

Using 'eval' also works for the example you give:

        eval command args 2> $logfile

Again, I'm not against /dev/stderr, it just doesn't exist in Tru64 UNIX
and I'm attempting to offer things that will work and things that
should be portable to other operating systems that support the bourne
and korn shells.



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2000-02-25  5:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2000-02-23  5:00 no /dev/stderr ? Usman Muzaffar
2000-02-24  5:00 ` Bob Harris
2000-02-25  5:00   ` Timo Felbinger
2000-02-25  5:00     ` Bob Harris

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox