Catching errors in a shell scripts
Matthew Flaschen
matthew.flaschen at gatech.edu
Sun Apr 8 03:45:31 UTC 2007
Simon Skogh wrote:
> Matthew Flaschen wrote:
>> 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
>
> Can't agree more. Now I'm just trying to beat sed into submission
> (we've never been on good terms) to make the output escape single
> quotes, i.e. turn ' into \' so the queries won't break
Like:
matthew at e1505-laptop:~/Code/Shell% echo "test='boo'">test.txt
matthew at e1505-laptop:~/Code/Shell% sed -i "s/'/\\\\'/g" test.txt
matthew at e1505-laptop:~/Code/Shell% cat test.txt
test=\'boo\'
? I have no idea why you need that many backslashes (probably the shell
is also processing it), but it seems to work. :)
Matt Flaschen
More information about the ubuntu-users
mailing list