Catching errors in a shell scripts

Matthew Flaschen matthew.flaschen at gatech.edu
Sun Apr 8 02:16:01 UTC 2007


Simon Skogh wrote:
> Matthew Flaschen wrote:
>> I see.  That's because standard output and standard error are different
>> streams, and `command` (incidentially I find the equivalent $(command)
>> easier to type) apparently doesn't capture standard error.  The below
>> works, but there may well be a less hackish way.  This is a demo for
>> capturing the error from trying to copy a non-existent file.  For all
>> commands, it will capture both error and output to alloutput:
> 
>> tempoutput=`mktemp`
>> cp nonexistent_file backup &> tempoutput;
>> alloutput=`cat tempoutput`
>> rm $tempoutput;
>> echo "All output: $alloutput"
> 
>> mktemp generates a random filename, which helps prevent shenanigans.
> 
>> Matt Flaschen
> 
> 
> The simplicity of this has me wanting to beat my head with a frying
> pan.

My experience is, the simpler the problem, the less likely I can figure
it out without help.  This one actually took me a few minutes, though.

Matt Flaschen




More information about the ubuntu-users mailing list