Operating System - HP-UX
1834744 Members
2890 Online
110070 Solutions
New Discussion

Re: jumbled stdout and stderr.

 
Sanjay Yugal Kishore Ha
Frequent Advisor

jumbled stdout and stderr.

Hello everyone,

We have a script that redirects a command's stdout and the stderr output onto a file.
via. [cmd] > $tmpfile 2>&1

The tmpfile location is declared earlier in the script.

I understand that in such a case the stdout and stderr entries are always separated by newline
-----------------
stdout_entries
stderr_entries
-----------------

Now, my question -
1. can there be a case when both the stdout and stderr entries exist in the same line i.e. stdout_entrystderr_entry
2. One of our partners is getting such a state on his system, erratically though. Can this be by any chance be an indication of system's health?

Thanks,
Sanjay
Dying is the last thing that I will do.
4 REPLIES 4
Mark Grant
Honored Contributor

Re: jumbled stdout and stderr.

You can do this but you have to write for it. If the any output does not have a new line at the end (for example /usr/bin/echo "hello\c") then the next thing written to the file will appear on the same line.

In other words, the OS does not put a newline at the end of each line being written to the file, the application writing to it does. If the application doesn't put a new line at the end of a line then lines will get "jumbled up".

Generally what probably isn't happening is that the stdout and stderr are getting mixed up unless, two applications are writing to the same file and both have set non-blocking mode on (and probably a few more options too). I suspect that isn' the case.
Never preceed any demonstration with anything more predictive than "watch this"
Sanjay Yugal Kishore Ha
Frequent Advisor

Re: jumbled stdout and stderr.

Thanks Mark.

So in my case, the application in question would be the cmd whose stdout and stderr is being redirected to the temp file.
Very well, I follow this lead as well.

Thanks,
Sanjay
Dying is the last thing that I will do.
Mark Grant
Honored Contributor

Re: jumbled stdout and stderr.

Yes, or, if its a script, it might be any command within the script. It should be pointed out that most shell commands put a new line at the end of their output by default.

Interestingly, perl doesnt so if you have any perl involved in this story, I'd look there first.
Never preceed any demonstration with anything more predictive than "watch this"
Michael Schulte zur Sur
Honored Contributor

Re: jumbled stdout and stderr.

Hi,

what about this?

[cmd] 2>&1 | awk '{print $0}' > $tmpfile

Michael