LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Error handling of sections in Bash (https://www.linuxquestions.org/questions/linux-software-2/error-handling-of-sections-in-bash-4175540096/)

Basher52 04-18-2015 04:04 PM

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:

{
cp      -pr "$PDdir"/PD-dsa.pem $bkupdir$PDdir &&
cp      -pr /data/install/install.cfg $bkupdir$PDdir &&
} || {
        echo "BKUP ERROR: Error in copying dirs/files to $bkupdir$PDdir" key
}

I've tested this but it won't work or if I'm doing something wrong with it, donno.

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.

kmhuntly 04-18-2015 04:41 PM

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.

jpollard 04-18-2015 04:42 PM

You mean something like:
Code:

( cp xyz /tmp &&
  cp wxy /tmp
)
if [ $? -ne 0 ]; then
  echo "error copying"
fi

With this, the exit status of the subshell will be which ever "cp" fails. If the first fails, the second will not be processed, if the second fails, the "error copying" message is shown, but the first copy will also have been done.

kmhuntly 04-18-2015 04:46 PM

Yup, should work. I usually do per command instead of multiple in a subshell but yeah .. Should work =)

Basher52 04-18-2015 05:08 PM

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 :)

kmhuntly 04-18-2015 05:28 PM

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

metaschima 04-18-2015 05:31 PM

The way I do it is I first declare an error function, then I just chain all the commands together:

Code:

error() # error
{
    echo "ERROR $1"
    exit 1
}

cp -pr "$PDdir"/PD-dsa.pem $bkupdir$PDdir && cp -pr /data/install/install.cfg $bkupdir$PDdir || error "Error in copying dirs/files to $bkupdir$PDdir key"


Basher52 04-18-2015 06:59 PM

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.