how to get messages originally output to stderr after it has been
how to get messages originally output to stderr after it has been
am 08.04.2008 10:38:35 von PRC
Hi All,
Have a look at this script:
STDERR=/tmp/.stderr
exec 3<>$STDERR 2>&3;
date >&2
ls abc.k | gawk '{print 5}'
gcc
ld
read -u3 aa bb cc
echo $aa-$bb-$cc
exec 3<&-
echo -----------------------
echo
echo -----------------------
cat $STDERR
All error messages are redirected to the file /tmp/.stderr. But why
`read' get nothing from file descriptor 3?
How to modify this script to have `read' get something from file
descriptor 3?
BTW, must file descriptor 3 be associated with a real file in the
system? If the line
exec 3<>$STDERR 2>&3;
is modified to
exec 2>&3;
bash will report a `Bad file descriptor' error.
Best Regard
PRC
Apr 8, 2008
Re: how to get messages originally output to stderr after it has been redirected?
am 08.04.2008 14:05:39 von PK
PRC wrote:
> Hi All,
>
> Have a look at this script:
> STDERR=/tmp/.stderr
> exec 3<>$STDERR 2>&3;
> date >&2
> ls abc.k | gawk '{print 5}'
> gcc
> ld
> read -u3 aa bb cc
> echo $aa-$bb-$cc
> exec 3<&-
> echo -----------------------
> echo
> echo -----------------------
> cat $STDERR
I assume you're using bash...
> All error messages are redirected to the file /tmp/.stderr. But why
> `read' get nothing from file descriptor 3?
> How to modify this script to have `read' get something from file
> descriptor 3?
Not exactly sure why (hopefully someone more fd-savvy will explain that),
but it seems that using two different descriptors for input and output
works, ie
$ cat script.sh
STDERR=/tmp/.stderr
exec 3>"$STDERR" 4<"$STDERR" 2>&3
date >&2
ls abc.k | gawk '{print 5}'
gcc
ld
read aa bb cc <&4
echo $aa-$bb-$cc
exec 3<&- 4<&-
echo -----------------------
echo
echo -----------------------
cat "$STDERR"
$ ./script.sh
Tue-Apr-8 14:02:16 CEST 2008
-----------------------
-----------------------
Tue Apr 8 14:02:16 CEST 2008
ls: cannot access abc.k: No such file or directory
gcc: no input files
ld: no input files
> BTW, must file descriptor 3 be associated with a real file in the
> system? If the line
> exec 3<>$STDERR 2>&3;
> is modified to
> exec 2>&3;
> bash will report a `Bad file descriptor' error.
That is correct, since there is no descriptor 3 open.
--
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.
Re: how to get messages originally output to stderr after it has been
am 08.04.2008 17:45:14 von spcecdt
In article <9887299b-2fe7-4706-8764-90bd1ea97a21@p39g2000prm.googlegroups.com>,
PRC wrote:
>STDERR=/tmp/.stderr
>exec 3<>$STDERR 2>&3;
>date >&2
>ls abc.k | gawk '{print 5}'
>gcc
>ld
>read -u3 aa bb cc
>echo $aa-$bb-$cc
>exec 3<&-
>echo -----------------------
>echo
>echo -----------------------
>cat $STDERR
>
>All error messages are redirected to the file /tmp/.stderr. But why
>`read' get nothing from file descriptor 3?
Because the file pointer is positioned after the last data written - the end of
the file. If you use ksh93, you can see this by adding just above the read
line:
echo $(3<#)
which will show the current offset.
>How to modify this script to have `read' get something from file
>descriptor 3?
If you're using ksh93, add just before the read::
3<#((0))
This will reposition the file pointer to offset 0, the start of the file.
In other shells - close and reopen fd 3.
>BTW, must file descriptor 3 be associated with a real file in the
>system? If the line
>exec 3<>$STDERR 2>&3;
>is modified to
>exec 2>&3;
>bash will report a `Bad file descriptor' error.
You need to dup a file descriptor from an existing file descriptor.
And yes, a file descriptor can only be created for something that supports
file operations (open, etc.).
John
--
John DuBois spcecdt@armory.com KC6QKZ/AE http://www.armory.com/~spcecdt/
Re: how to get messages originally output to stderr after it has been redirected?
am 08.04.2008 18:42:31 von Maxwell Lol
PRC writes:
> ls abc.k | gawk '{print 5}'
Odd bit of scripting.....