LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-15-2009, 07:57 AM   #1
pendal
LQ Newbie
 
Registered: Jun 2009
Posts: 12

Rep: Reputation: 0
nice little shell script needs some "error checking" help


I have this little script i wrote to use the diff command to check two directories, if they are exactly the same the first one is deleted.

here is the script as it sits now.

Code:
#! /bin/bash

diff -qr "$1" "$2" > /dev/null
v1=$?

if [ $v1 = 0 ]
then
rm -Rf "$1"
echo "$1 was removed"
else
SUBJECT="Did not Remove a folder"
TO="myemail@address.com"
MESSAGE="/msg/checkdiffmsg.txt"

echo "Not Right. Did Not Remove The Directory" >> $MESSAGE
echo "$1" >> $MESSAGE

/usr/bin/mail -s "$SUBJECT" "$TO" < "$MESSAGE"

rm $MESSAGE
fi
the script is called using this command line

checkdiff.sh dir1 dir2

the script is working well with one problem.

when the rm command is issued, sometimes the files are locked and the directory cannot be removed.

is there a way for me to check for this? or at least to shoot myself an email and tell me that it did not happen?

Thanks in advance.

-C
 
Old 10-15-2009, 10:02 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,609

Rep: Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960
Quote:
Originally Posted by pendal View Post
I have this little script i wrote to use the diff command to check two directories, if they are exactly the same the first one is deleted.


the script is called using this command line

checkdiff.sh dir1 dir2

the script is working well with one problem.
when the rm command is issued, sometimes the files are locked and the directory cannot be removed.
is there a way for me to check for this? or at least to shoot myself an email and tell me that it did not happen?
You could always check the return status of the RM command, and if it's bad, mail a message out. From the info page on the 'rm' command:
Code:
An exit status of zero indicates success, and a nonzero value
indicates failure.
obtained via this command: info coreutils 'rm invocation' on openSUSE
 
Old 10-18-2009, 11:06 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
how about, check the directory after, if it's still there it's an error.
then send a mail.

at the end of the script...

Code:
dir=/blah/blah/dir 

[ -d "$dir" ] || exit  # it isn't there anymore

mail -s "dir remove failed" $USER  << EOF

       $(date)
       Failed to remove the following file(s)
       
$(find $dir)
 
EOF
 
  


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
./configure script hangs at "checking for ICMP ping syntax..."l khilari Linux - Software 4 11-12-2011 10:27 AM
script using "/usr/bin/cat error" produces "cannot open" in cron Dcrusoe Programming 6 07-22-2009 03:30 PM
Checking multiple systems on network with command "top" using a Linux Script Echo Kilo Programming 16 06-15-2008 06:33 PM
converting shell script from "dialog" to "xdialog" kushalkoolwal Programming 1 02-25-2008 08:40 PM
Shell Script: Find "Word" Run "Command" granatica Linux - Software 5 07-25-2007 07:42 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:08 PM.

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