Shell script for creating log file and sending mail?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script for creating log file and sending mail?
# 1  
Old 02-27-2012
Shell script for creating log file and sending mail?

Hi ,

I am trying to create shell script which will help me to compare file name in two folder.
There is a multiple file store in 2 folder.I want to compare that with the name.
If all the file are same then send a mail that "all date is same"

if not then create one log file which contain non-matching file name and send that file with the subject "all contain are not same.

I have tried to compare two file but dont know how to create log file and send a mail.Please find the below script and help me to complete this.

Quote:
# Read dir names
echo "Enter first dir name(absolute path).."
read first_dir
echo "Enter second dir name(absolute path).."
read second_dir

# check for the existance of dir..
if [ ! -d $first_dir ]; then
echo "$first_dir does not exists.."
exit 1
fi
if [ ! -d $second_dir ]; then
echo "$second_dir does not exists.."
exit 1
fi

# compare dirs..
for second_dir_file in `ls $second_dir`
do
# If second_dir_file is present in first_dir
if [ -f "$first_dir/$second_dir_file" ]; then
echo "$second_dir_file is present in $first_dir.."
echo .
echo "overwriting $second_dir_file in $first_dir "
cp -f $second_dir/$second_dir_file $first_dir/$second_dir_file
# If second_dir_file is not present in first_dir
else
echo "$second_dir_file is not present in $first_dir.."
echo .
fi
done

# If first_dir_file is not present in second_dir
for first_dir_file in `ls $first_dir`
do
# If first_dir_file is not present in second_dir
if [ ! -f "$second_dir/$first_dir_file" ]; then
echo "$first_dir_file is not present in $second_dir.."
echo .
fi
done

Thanks....
# 2  
Old 02-27-2012
Check into the command 'comm', which takes 2 files or pipes of binary sorted lines, and marks them for one of three columns: a-only, b-only or both. It slides lines left to columns 2 and 3 using 1 or 2 tabs. If you option it -3, it removes all lines in the both column, -13 says remove a and both, leaving no tabs and just what is in file b only. Think of it as robust set arithmetic. In ksh and bash, a neat form sorting on a generated name pipe is:
Code:
( export LC_ALL=C ; comm -13 <( sort file-a ) <(sort file-b ) )

The LC_ALL not C can upset newer sort versions so the sort order can be very strange if not broken.

You can easily post-process the comm output to differentiate a-not-b from b-not-a. The sort input does not need to be a file, it could be:
Code:
comm -3 <(
    cd dir-a ; find * -type f | sort
  ) <(
    cd dir-b ; find * -type f | sort
  )| sed '
        s/^\t/B:/    # \t here represents an actual tab in the sed script
        t
        s/^/A:/


Last edited by DGPickett; 02-27-2012 at 06:43 PM..
# 3  
Old 02-28-2012
Hi DGPickett,

Thanks... for your quick response...But, I am not sure what you are trying to say and how it will create logfile and send the mail.Please explain me in detail.

Thanks....
# 4  
Old 02-28-2012
Your script in pawing through files and dirs seeing which are not present in both. Comm can do that in bulk, wholesale, identifying which are not present in both. You can tee the output to a log file as you pipe it to mail:
Code:
export zid=$(date "+MissingFiles-%Y-%m-%d-%H%M%S") LC_ALL=C
comm -3 <(
     cd dir-a ; find * -type f | sort
   ) <(
     cd dir-b ; find * -type f | sort
   )| sed '
         s/^\t/B:/    # \t here represents an actual tab in the sed script
         t
         s/^/A:/
 ' | tee $zid_log.txt
) | mail -s $zid you@your_domain


Last edited by DGPickett; 02-28-2012 at 10:52 AM..
# 5  
Old 02-29-2012
Hi DGPickett,
Again I would like to thank you for your help and explanation….
The requirement is something like that; I am getting a file every month which need to be compare with the last month file.
1. New File is storing in some location in zip formate.I wanted to unzip that and store in some other location.
2. Old file is available in other location like archive folder. Which is used for comparison with new file?
3. I wanted to compare this two file and create the log and send sending the mail. Which is working?
4. After this process overwrite the old file with the new for next month comparison.
One more help: could you please explain the first line of the code which you provided.
export zid=$(date "+MissingFiles-%Y-%m-%d-%H%M%S") LC_ALL=C


Looking for the help on this issue.

Thanks....
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

A shell script for checking the last image and sending it in attachment to mail

Hello every one. I use mutt : "mpack -s "Test" /home/pi/Pictures/2018-10-05_23_10_40.jpg my_email_addres " to send me a particular picture with the name of the data+jpg in a pictures folder, but this folder I use it for a timelapse proyect with more pictures , and what I need is... (2 Replies)
Discussion started by: maxbcn
2 Replies

2. Shell Programming and Scripting

Configuring smtp settings and then sending the mail through shell script

I have make an menu in which first option is to start and second is to stop the services echo "Please enter the appropriate choice for doing the operations" echo " 1) STOP Services 2) START Services case $choice in 1) ... (1 Reply)
Discussion started by: punpun66
1 Replies

3. Shell Programming and Scripting

Sending Mail via shell script

I am in need of a script that will send out emails while going through a NAT. What I have that works is as follows: display_remote_IP | sort | uniq | while read i do FOUND=0 for IP in `echo $ACCEPTABLEIP` do if ; then FOUND=1... (2 Replies)
Discussion started by: ldapguy
2 Replies

4. Shell Programming and Scripting

Sending mail from shell script

Hello All, I m trying to send mail from my unix script, I have used the below command mailx -s 'hi' email address < temp.txt It is not giving me any error,but I couldn't receive the mail Can you please help me. Many Thanks, Pragyan (6 Replies)
Discussion started by: prarat
6 Replies

5. Shell Programming and Scripting

shell script not sending mail

Hi, I have shell script which checks the filesystem check but does not send the mail. I might be missing something very simple.. could you please help? #!/bin/sh _servers="machine name" _out="/tmp/output.$$" _email="me@there.com" _sub="Disk Space Report $(date)" _mail=/usr/bin/mail... (5 Replies)
Discussion started by: Pandee
5 Replies

6. Shell Programming and Scripting

Reading a file and sending mail by shell scripting?

hi I need help urgently...i need to write a shell script which can solve the following problem....its urgent plz help me out coz m totally newbie in shell scripting.... the problem is: Suppose I have a folder called logs. whenever some error occurs some correspondence error file is generated. I... (4 Replies)
Discussion started by: sukhdip
4 Replies

7. Linux

Memory monitoring and sending alert mail to users in network using shell script

i m workiing on a shell script which may monitors network memory and send alert to user if it increase a threshold (1 Reply)
Discussion started by: navdeep5673
1 Replies

8. Shell Programming and Scripting

Shell script for sending automatic email to personal mail id

hi guys, I need a shell script to send mail automatically to my personal mail id like xxxx@hotmail.com but while experimenting with "mail" command I faced following problems. cat text1.txt | mail -s 'test mail' xxxx@hotmail.com command successfully executed but while checking for... (4 Replies)
Discussion started by: rrd1986
4 Replies

9. Shell Programming and Scripting

sending a mail using shell script

Please help me in writing the script for sending an attachment through email.(For example my text file name is :abc.txt and it is in directory d:/abc) (1 Reply)
Discussion started by: anitha126
1 Replies

10. Shell Programming and Scripting

Help with a shell script for creating a log file

I have a schell script that runs continously on an AIX system. It is actually started from another shell script with the "ksh -x" command and then I just write the output to a log file. This causes the log files to be filled with mostly useless information. I would like to modify this script to... (2 Replies)
Discussion started by: heprox
2 Replies
Login or Register to Ask a Question