LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-21-2015, 05:47 AM   #1
AntBla
LQ Newbie
 
Registered: Oct 2012
Posts: 16

Rep: Reputation: Disabled
What is the best way to trap an error in a script


Hi All.

I am running a script that copies files to a USB disk.

What I would like to do is run another script if the USB disk get disconnected before the copying is done.

TIA.

Ant.
 
Old 02-21-2015, 07:42 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,246

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
And what have you done in an attempt to solve this problem?
 
Old 02-21-2015, 08:18 AM   #3
AntBla
LQ Newbie
 
Registered: Oct 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
Dear Grail.

Thanks for your helpful reply. I did not realise that posting what I had already tried was essential.

Anyway, what I do now is at the end of the script I check to see if the USB device is still present and if not, run the error script.

The problem with this is that the device may still be present, but there was a write error or some other issue with the cp command.

What I was hoping for was a more sophisticated solution so that when/if the cp command fails, it jumps straight to the error.

Unless you can lead me to a better process, I guess I will stick with this.

Ant.
 
Old 02-21-2015, 04:11 PM   #4
GNU/Linux
Member
 
Registered: Sep 2012
Distribution: Slackware-14
Posts: 118

Rep: Reputation: Disabled
It would help if you actually post the script. Use code tags (Click 'Post Reply' then click on '#') for that.
 
Old 02-21-2015, 04:54 PM   #5
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,602

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
One problem you have is that if the device gets unplugged, you may have corrupted the filesystem on it...

Now if your trap handler does nothing but request the user to put it back in... things MIGHT be able to continue. Normally, this is not handled by the shell though - it is handled by the USB controller so that any copy to/from isn't aborted, and the system buffers remain consistent (and the storage device doesn't get corrupted). If, howerever, the USB actually resets, I think things can get nasty - the device is now corrupted, and might not be mountable.

I believe the usual way to trap shell errors within the shell (and not abort the script) is by using an "eval" on a function. If the function aborts, the eval will catch it and return the error status.
 
Old 02-21-2015, 04:59 PM   #6
AntBla
LQ Newbie
 
Registered: Oct 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
Here is the script.
What I would like to do is somehow trap is the cp command did not complete, then run the error.sh script.
Thanks for looking at it.
Code:
#!/bin/bash
cp -r  /usr/local/data  /media/usb0/backup/
check=$(find /media/usb0 -name 'check.txt')
if test -z "$check";then
sh /usr/local/bin/error.sh
exit
else
sh /usr/local/bin/complete.sh
fi
sync
umount /media/usb0
 
Old 02-21-2015, 05:53 PM   #7
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,602

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
Umm... you can't check for a file on a removed device without having yet another failure...

All you can do is evaluate the exit status of the cp.
 
Old 02-21-2015, 06:51 PM   #8
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 2,403

Rep: Reputation: Disabled
Quote:
Originally Posted by AntBla View Post
Here is the script.
What I would like to do is somehow trap is the cp command did not complete, then run the error.sh script.
Thanks for looking at it.
Code:
#!/bin/bash
cp -r  /usr/local/data  /media/usb0/backup/
check=$(find /media/usb0 -name 'check.txt')
if test -z "$check";then
sh /usr/local/bin/error.sh
exit
else
sh /usr/local/bin/complete.sh
fi
sync
umount /media/usb0
If the cp command fails for some reason, it will exit with a non-zero exit code. The actual exit code will be stored in the $? variable which you can then test for a non-zero value:
Code:
cp -r /usr/local/data /media/usb0/backup/
if [ $? -eq 0 ]; then
  # all is well
else
  # something went wrong
fi
Or you can simply use the cp command as a condition in the if statement:
Code:
if cp -r /usr/local/data /media/usb0/backup/ ; then
  # all is well
else
  # something went wrong
fi
Also, as jpollard pointed out, checking whether a device is mounted or not by looking for a specific file, is perhaps not the best idea: If the device is not mounted, you'll generate another error. The method seems to have the advantage of being able to identify a specific device as being mounted (if the file is there), but that assumes that the file hasn't been deleted, and that a file by the same name was not created on a different device.

If you want to see whether a device is mounted at a specific mount point, I'd recommend parsing the output from the mount command. If the path to the mount point is listed, a device of some kind must obviously be mounted there.

It's more difficult to verify conclusively that a specific device is mounted at that mount point. You could check for a filesystem UUID, but I believe that information can only be read with a filesystem-specific utility. For instance, tune2fs is able to extract the UUID from an ext2/3/4 filesystem.

Checking for a particular partition table GUID could be an alternative, but AFAIK those are only found on GPT-partitioned disks.

Last edited by Ser Olmy; 02-21-2015 at 06:53 PM.
 
1 members found this post helpful.
Old 02-21-2015, 10:40 PM   #9
AntBla
LQ Newbie
 
Registered: Oct 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
Thanks Ser Olmy for your detailed reply.
This solution
Code:
cp -r /usr/local/data /media/usb0/backup/
if [ $? -eq 0 ]; then
  # all is well
else
  # something went wrong
fi
works perfectly and has really helped me out.

Ant.
 
  


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
script to trap SIGNIT workout74 Linux - Newbie 2 07-13-2012 06:45 AM
Trap a user in a shell script kelso.b Programming 6 04-20-2012 12:54 PM
[SOLVED] Using trap in bash script sree_ec Programming 13 03-28-2012 11:48 PM
how to send snmp trap & recieve trap in C program minil Programming 3 07-10-2010 10:22 AM
Shell script trap and log out a user fmcauley Programming 6 03-06-2007 11:10 PM


All times are GMT -5. The time now is 06:46 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration