Shell : quick newb question - redirecting to file

Stephen R Laniel steve at laniels.org
Tue Jul 12 11:13:26 UTC 2005


On Tue, Jul 12, 2005 at 01:04:16PM +0200, Vincent Trouilliez wrote:
> I tried this command : "strace yelp > ~/yelp.strace "
> 
> It does start Yelp, and print loads of stuff in the terminal window, and
> it does create the 'yelp.strace' file, but, once I quit Yelp/strace, and
> go to have a look at the result, the text file is empty... zero byte :-/

Your first warning sign was that you got loads of stuff in
the terminal window. Presumably you wanted all of that stuff
to go into your logfile.

Have you learned about 'standard output' and 'standard
error'? Every UNIX program gets three filehandles when it
starts, by default: standard input ('stdin'), standard
output ('stdout') and standard error ('stderr'). The first
is used for ... input. The last is used to print error
messages to the console. The second is used to output
everything else (where 'everything else' normally includes
things like prompts for the user).

These filehandles are numbered: stdin is filehandle 0,
stdout is filehandle 1, and stderr is filehandle 2.

SO (back to your question): when you do

programName > outputFile

you're actually doing

programName 1> outputFile

which means "take filehandle 1 -- stdout -- and dump it to a
file."

Have you spotted the problem? You're not dumping out stderr.
And stderr may contain lots of stuff that you want to dump
to a logfile.

So you need some way to dump stderr and stdout at the same
time. Here's how you do it:

programName 2>&1 >outputFile

This says "Take filehandle 2 and send it to the same place
where you send filehandle 1." Then the second '>' says "Now
take filehandle 1 and send it to outputFile." Voila: both
stderr and stdout are going to outputFile.

You'll know this has worked correctly when nothing prints to
the console.

-- 
Stephen R. Laniel
steve at laniels.org
+(617) 308-5571
http://laniels.org/
PGP key: http://laniels.org/slaniel.key
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: <https://lists.ubuntu.com/archives/ubuntu-users/attachments/20050712/c609111b/attachment.sig>


More information about the ubuntu-users mailing list