Catching errors in a shell scripts
Simon Skogh
simon at swedishdrunkard.com
Sun Apr 8 05:46:28 UTC 2007
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Matthew Flaschen wrote:
> 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
>
Damnit, I only went to three slashes, haha.
Thanks for all the help, Matt!
Simon
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFGGIG0z+/vk9aBZhMRAg5KAJ9pLsjbQOlxUZac92GCWn2/lsVUVgCfflfy
O8l4N1ubHhtMzt7KvsXB4rw=
=W2qC
-----END PGP SIGNATURE-----
More information about the ubuntu-users
mailing list