Error handling of sections in Bash
I've been looking a while to see if there is way to create a smart error handling in a bash script. Nothing has popped up so I figured that you guys gotta know if it's even possible.
Let's say we have a script that has a section of copying files and instead of testing if every copy is OK I want to know if there is a way to test this "section" of copying. ... or if you have a section in the script that deletes files and directories and here too, instead of testing everyone, check to see if there are errors in that. I've seen things that you can put everything withing curly brackets '{}' something like this: Code:
{ So what I want is an easy way to test sections for errors/warnings instead of test each row that just makes the script unreadable. |
You can check the return code with ${?}, capture stderr to a variable and interrogate with 2>file, or (given your example) you can verify the target file exists and then compare checksums. I use a combination of retcode checking and checksumming for file copies, I do use stderr but only for logging as there are apps out there that write to stderr even if everything is OK.
|
You mean something like:
Code:
( cp xyz /tmp && |
Yup, should work. I usually do per command instead of multiple in a subshell but yeah .. Should work =)
|
OK, thanks :D
Think I'll go with jpollard's version first off to test it. kmhuntly, think you can give an example of what you mean by your version, seems very interesting. I just can't seem to get those stderr things to work when I do 'em. Not the verifying of the copied file though just the retcode-thingy :) |
Error handling of sections in Bash
I think this script should have everything you want in it -
https://github.com/cwsus/cws-esolutions/blob/master/Shell/DNSAdministration/lib/executors/executeKeyGeneration.sh |
The way I do it is I first declare an error function, then I just chain all the commands together:
Code:
error() # error |
thx all.
I'll try all your thoughts about this and think I'll get a script that works :D |
All times are GMT -5. The time now is 02:14 AM. |