is stderr buffered in ksh?

is stderr buffered in ksh?

Post by Donald Mitchel » Fri, 19 Dec 1997 04:00:00



I have a ksh script that redirects stderr to a file:

exec 2>> ${ERROR_FILE}

at the end of the script I want to remove the ERROR_FILE if it is empty
(so I don't have a bunch of zero byte files sitting around).  Is it ok
for me to do the following:

if [ ! -s "${ERRORFILE}" ]
then
    # empty file...so no errors!
    /usr/bin/rm -f ${ERRORFILE}
else
    # write date to error file (for debugging purposes)
    /usr/bin/date >&2
    echo >&2
fi

What I'm wondering is whether or not I have to close stderr before
checking that it is empty.  None of the commands in the shell script are
ran in the background.

Is there a better way to do this?

Thanks,
Don

--
Donald S. Mitchell
AT&T
Voice: (732) 805-7604
Fax: (732) 805-7320

 
 
 

is stderr buffered in ksh?

Post by d.. » Sat, 20 Dec 1997 04:00:00


On Thu, 18 Dec 1997 09:50:47 -0500, Donald Mitchell


>I have a ksh script that redirects stderr to a file:

>exec 2>> ${ERROR_FILE}

>at the end of the script I want to remove the ERROR_FILE if it is empty
>(so I don't have a bunch of zero byte files sitting around).  Is it ok
>for me to do the following:

>if [ ! -s "${ERRORFILE}" ]
>then
>    # empty file...so no errors!
>    /usr/bin/rm -f ${ERRORFILE}
>else
>    # write date to error file (for debugging purposes)
>    /usr/bin/date >&2
>    echo >&2
>fi

>What I'm wondering is whether or not I have to close stderr before
>checking that it is empty.  None of the commands in the shell script are
>ran in the background.

to quote from
setvbuf()

  By default, output to a terminal is line-buffered and all other
    input/output is fully buffered.

perhaps judicious use of
2>&- is called for?

 
 
 

is stderr buffered in ksh?

Post by Barry Margoli » Sat, 20 Dec 1997 04:00:00




Quote:>What I'm wondering is whether or not I have to close stderr before
>checking that it is empty.  None of the commands in the shell script are
>ran in the background.

Since this is a shell script, it doesn't really matter.  All buffers are
flushed whenever a command exits, since the buffers live in the memory of
each process.

Also, stdio makes a special exception to its normal buffering rules for
stderr.  All other streams are fully buffered unless they're going to a
terminal, but stderr is line buffered by default.

So it should be safe for your script to check the file before closing it.

--

GTE Internetworking, Powered by BBN, Cambridge, MA
Support the anti-spam movement; see <http://www.cauce.org/>
Please don't send technical questions directly to me, post them to newsgroups.

 
 
 

1. ksh redirect stdout & stderr > file1, stderr > file2

In korn shell, how can I redirect standard output and standard error
to one file, and just standard error to another?

I've been trying things like flipping the file descriptors for stdout
& stderr, redirecting the new stderr to an outfile & piping stdio to
tee for output to both files.   An example of an attempt is below:

exec 3>&1
exec 1>&2
exec 2>&3
myscript 2> stdlog | tee errlog >> stdlog

Thanks..
-Ken

2. 2600 in Denver/303 AC

3. What's 'side effects' of Ksh built-ins?

4. TokenRIng: the ARRG problem...

5. how to bash redirect stdout stderr without buffering

6. installing driver??

7. Redirect stdout and stderr in ksh

8. how to pass return value from korn shell to c program

9. filtering stdout and stderr in ksh??

10. stdout & stderr direction in "ksh"

11. How to pipe stderr in ksh.

12. piping stderr in Bourne/ksh

13. Redirecting stderr in ksh