Operating System - HP-UX
1753912 Members
8655 Online
108810 Solutions
New Discussion юеВ

Re: ksh script, unbuffering(?) output

 
SOLVED
Go to solution
Ben Spencer
Occasional Advisor

ksh script, unbuffering(?) output

I am trying to capture the output from a program and display the output on the display, AND check the return status from the program. I have managed to figure out at least on way to do this (see statement below) but there is one problem: the output to both the screen and the file is buffered (apparently) and is not shown in a timely manner. I would like the output to appear on the screen as soon as it is generated. Is there a way to force the buffer to be flushed after each line displayed? If I don't use the subshell and just redirect the output to tee I think the ouptut shows up in a timely way. For certain it does if I just put the output to the screen. Thanks.

here is the statement I am using: (/opt/omni/sbin/omnidbutil -readascii -mmdb $workdir -cdb $workdir;echo $?>$raerrcd) <&1 | tee -a $fileout
Y
EOF

I echo the return status ($?) to the file $raerrcd inside the subshell so that I can retrieve it and check it later. Outside the subshell I redirect all the output to tee so that output goes to both the display and the file $fileout. In addition there is input required by the program that is provided in the last two lines via the redirection <
5 REPLIES 5
Sam Nicholls
Trusted Contributor
Solution

Re: ksh script, unbuffering(?) output

The problem is with 'tee'. I don't think there is any way to make it not buffer. If you have gawk, you can do something like...

(omnidbutil -readascii -mmdb $workdir -cdb $workdir;echo $?>$raerrcd) <&1 | awk '{print >> "outfile";print;fflush("")}'

Regular awk doesn't support fflush().
-sam
Marcel_3
New Member

Re: ksh script, unbuffering(?) output

Hi,

Not sure if this would work, but if the problem is with the tee, could you wrap your statements in a script command?

This should get all the output to the logfile and, I think, to the display as well. As far as I know there's no buffering.

Marcel
Carlos Fernandez Riera
Honored Contributor

Re: ksh script, unbuffering(?) output


You can control the this jobs with tail -f

(/opt/omni/sbin/omnidbutil -readascii -mmdb $workdir -cdb $workdir;echo $?>$raerrcd) < $fileout 2>&1 &

tail -f $fileout





unsupported
Ben Spencer
Occasional Advisor

Re: ksh script, unbuffering(?) output

Thanks for the replies. I am not sure we have gawk so I might try the same thing in perl.

I had thought about the tail option but did really like the idea of looping through a tail for 6 hours till the background job completed.

I am intrigued by Marcel's idea, but I am not sure what you mean by wrapping the statements in a script command? These statements are already in a shell script with a few other housekeeping statements. How do I wrap them in a script command?

Thanks.
Marcel_3
New Member

Re: ksh script, unbuffering(?) output

Hi,

Sorry, looks like I've caused confusion.

Your running a shell script, but, if before you run it you type:

script logfile.log

and then do a Ctrl-D after your shell script has finished you'll end up with a file called logfile.log which should contain all the output from the shell script.

Of course, rather than typing it at the command line you can bung it in a wrapper for your existing shell script.

Cheers,

Marcel