# # Capture and report (Bash Scripting):Bash|Scripting|Basics::/Basics/capture_report.html
APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

Capture and report (Bash Scripting)

I've removed advertising from most of this site and will eventually clean up the few pages where it remains.

While not terribly expensive to maintain, this does cost me something. If I don't get enough donations to cover that expense, I will be shutting the site down in early 2020.

If you found something useful today, please consider a small donation.



Some material is very old and may be incorrect today

© November 2011 Anthony Lawrence

You've been asked to copy some jpg files to a USB disk overnight. That's easy enough - a cron job and a simple "cp -a" will do that.

But there is so much that could go wrong, isn't there? There might not be any files to copy or there might not be room on the USB disk. Somebody might have changed permissions on files or directories alreasdy in place, preventing overwrite with updated images.

Maybe your first thought was just to send all the output to a file that might get printed or mailed for someone to look at. That's easy:


#!/bin/bash
cd /Pictures 
cp -av *jpg /TINYUSB > /tmp/report 2>&>1
 

Though that could be a mighty long report and if nothing went wrong, who cares?

You could just capture the error output.


#!/bin/bash
cd /Pictures
cp -av *jpg /TINYUSB 2> /tmp/report 
 

That's better, but it doesn't have the "-v" output. Isn't there a way to get both?

Yes, there is, although it does involve a little more work. The script that follow does that and a few other checks.

#!/bin/bash
# these lines just make sure we are in the right place
cd /Pictures || echo "`date` Copy script can't find /Pictures!!" | lp
cd /Pictures || exit 1
ls *.jpg > /tmp/report
# if there is nothing to copy, /tmp/report will say so
# if there are files, /tmp/report is nulled out and will be empty
# make sure TINYUSB is mounted
/bin/mount | grep TINYUSB || echo "TINYUSB not mounted!" | lp
/bin/mount | grep TINYUSB || exit 1
for i in *.jpg
do
  cp -av "$i" /TINYUSB > /tmp/failed 2>&1
  # /tmp/failed will have both -v and error output
  # but we'll only copy it if there really was an error
  case $?  in
   0) : ;;
   *) echo "Exit code was $?" >> /tmp/failed;
      echo " " >> /tmp/failed;
      echo "`date` Copy of $i failed" > /tmp/message; 
      cat /tmp/message /tmp/failed >> /tmp/report;;
  esac
  grep -q "No space left on device" /tmp/report && lp /tmp/report 
  grep -q "No space left on device" /tmp/report && exit 1
  # no reason to go on if there is no space
done
test -s /tmp/report && lp /tmp/report
# if no errors. nothing will print because /tmp/report will still be empty
 

The "$?" in the case switch will be the exit code of the last cp command. If all went well, it will be 0 and we do nothing. If it's not 0, we'll add /tmp/failed to /tmp/report.

If the device has run out of space, there is no reason to keep trying, so we'll quit the script then and there.

There's no need to do that "cd /Pictures" or the "grep -q" twice.

  grep -q "No space left on device"   /tmp/report && { lp /tmp/report ;exit 1; }
 

Spaces after "{" and before "}" are necessary and that ";" after "exit 1" is also needed.

However, doing it twice makes it very obvious what you want to happen and why and saves you from forgetting the spaces or the semicolon.

There are probably other things that can go wrong that I haven't thought of today. If you know one, let me know and we'll add it.


If you found something useful today, please consider a small donation.



Got something to add? Send me email.





(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

->
-> Capture and report (Bash Scripting):Bash|Scripting|Basics::/Basics/capture_report.html


Inexpensive and informative Apple related e-books:

Take Control of iCloud, Fifth Edition

Take Control of IOS 11

Photos: A Take Control Crash Course

Take Control of Pages

iOS 8: A Take Control Crash Course





More Articles by © Anthony Lawrence





Printer Friendly Version

Have you tried Searching this site?

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us


Printer Friendly Version





Technology is both a tool for helping humans and for destroying them. This is the paradox of our times which we're compelled to face. (Frank Herbert)




Linux posts

Troubleshooting posts


This post tagged:

Basics

Scripting

Shell



Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode