LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 04-18-2015, 04:04 PM   #1
Basher52
Member
 
Registered: Mar 2004
Location: .SE
Distribution: Arch
Posts: 394

Rep: Reputation: 22
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.

Last edited by Basher52; 04-18-2015 at 04:25 PM.
 
Old 04-18-2015, 04:41 PM   #2
kmhuntly
Member
 
Registered: Mar 2015
Location: Cheektowaga, NY
Distribution: ArchLinux
Posts: 34

Rep: Reputation: 7
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.
 
1 members found this post helpful.
Old 04-18-2015, 04:42 PM   #3
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
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.
 
1 members found this post helpful.
Old 04-18-2015, 04:46 PM   #4
kmhuntly
Member
 
Registered: Mar 2015
Location: Cheektowaga, NY
Distribution: ArchLinux
Posts: 34

Rep: Reputation: 7
Yup, should work. I usually do per command instead of multiple in a subshell but yeah .. Should work =)
 
Old 04-18-2015, 05:08 PM   #5
Basher52
Member
 
Registered: Mar 2004
Location: .SE
Distribution: Arch
Posts: 394

Original Poster
Rep: Reputation: 22
OK, thanks
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
 
Old 04-18-2015, 05:28 PM   #6
kmhuntly
Member
 
Registered: Mar 2015
Location: Cheektowaga, NY
Distribution: ArchLinux
Posts: 34

Rep: Reputation: 7
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
 
1 members found this post helpful.
Old 04-18-2015, 05:31 PM   #7
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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"
 
1 members found this post helpful.
Old 04-18-2015, 06:59 PM   #8
Basher52
Member
 
Registered: Mar 2004
Location: .SE
Distribution: Arch
Posts: 394

Original Poster
Rep: Reputation: 22
thx all.
I'll try all your thoughts about this and think I'll get a script that works
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Detailed Error Handling In Bash LXer Syndicated Linux News 0 02-20-2013 07:31 AM
try/except error handling in bash desromic Programming 4 06-30-2011 03:04 PM
[SOLVED] bash error handling m3phisto Linux - Newbie 4 04-26-2011 07:28 AM
Bash Scripting handling user input sections automatically Gavin Harper Programming 3 06-18-2010 10:35 PM
Bash Script and Loop error handling Kedelfor Programming 5 05-22-2005 02:22 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration