Quote:
Originally Posted by AntBla
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.