UNIX scripting

LIST=/home/xxxxxxx/ABC/xeeno_scrpts/temp/tempfile
 ERRORDIR=/home/xxxxxxx/ABC/error_directory
 EMAILFILE=/home/xxxxxxx/ABC/xeeno_scrpts/temp/emailfile
 echo "There were errors in the following report file for Xeenos:" > $EMAILFILE
 echo >> $EMAILFILE
 echo "This files have been moved to $ERRORDIR" >>$EMAILFILE
 find /home/xxxxxxx/ABC/logs/*.rpt -mtime -8 | egrep -v ftplog >$LIST
 while read r1
 do
 grep CompletionCode $r1 >/dev/null 2>&1
 if [ $? = 0 ]; then
 grep CompletionCode $r1 | grep "CompletionCode: Value=0" >/dev/null 2>$1
 if [ $? = 1 ]; then
 echo $r1 >> $EMAILFILE
 echo >>$EMAILFILE
 mv $r1 $ERRORDIR
 fi
 fi
 for i in `cat /home/xxxxxxx/ABC/xeenos_scrpts/emailparm`
 do
 cat $ERRORDIR | mail -s $r1 $i@stp.aess.org
 done
 done<$LIST
 ~

---------- Post updated at 09:15 PM ---------- Previous update was at 09:14 PM ----------

I need help in this script. I want the outcome to finish with one file at a time in an email body

WHAT AM I doing wrong?

Could you please provide some more information ? What is desired o/p ?

I agree with pravin27 that there is no way to know what is wrong with this script if you don't clearly describe what it is supposed to do... But, there are some obvious problems:
Since you don't indent your code, it is somewhere between difficult and impossible to see the structure of your code.

  1. You create $MAILFILE before you start your loops (saying that errors have been detected (before you know if any errors have been detected) and add notes to that file during the outer loop. But the contents of that file are not cleared when you move on to a new file in the loop and the contents of that file (which one might assume from the name is to be e-mailed to someone is never used by this script to do anything but add lines to it) are never sent to anyone.
  2. Using cat to print the contents of a directory is generally a waste of time; a directory is not a text file. And, whether you use cat or the much more efficient file redirection to get the contents of a directory, input to the mail command needs to be text; not the contents of a directory.
  3. Do you really need to send individual e-mails to all of the recipients listed in emailparm ? Why not create a list of recipients outside the loop and just send one e-mail?
1 Like
LIST=/home/xxxxxxx/ABC/xeeno_scrpts/temp/tempfile
 ERRORDIR=/home/xxxxxxx/ABC/error_directory
 EMAILFILE=/home/xxxxxxx/ABC/xeeno_scrpts/temp/emailfile
 echo "There were errors in the following report file for Xeenos:" > $EMAILFILE
 echo >> $EMAILFILE
 echo "This files have been moved to $ERRORDIR" >>$EMAILFILE
 find /home/xxxxxxx/ABC/logs/*.rpt -mtime -8 | egrep -v ftplog >$LIST
 while read r1
 do
 grep CompletionCode $r1 >/dev/null 2>&1
     if [ $? = 0 ]; then
     grep CompletionCode $r1 | grep "CompletionCode: Value=0" >/dev/null 2>$1
           if [ $? = 1 ]; then
           echo $r1 >> $EMAILFILE
           echo >>$EMAILFILE
           mv $r1 $ERRORDIR
          fi
    fi
 for i in `cat /home/xxxxxxx/ABC/xeenos_scrpts/emailparm`
      do
      cat $ERRORDIR | mail -s $r1 $i@stp.aess.org
      done
 done<$LIST

---------- Post updated at 08:04 AM ---------- Previous update was at 08:02 AM ----------

the outcome of the script should be:

 There were errors in the following report file for Xeenos:

This files have been moved to /home/xxxxxx/ABC/error_directory
/home/xxxxxx/ABC/logs/20160505135628004.rpt

Sorry when I do the indent and submit the script it shifts everything to the left.

---------- Post updated at 08:54 AM ---------- Previous update was at 08:04 AM ----------

Don can you review the new message I posted and let me know how I can get the correct outcome?

Thanks,

I see that you chose to ignore all of the comments I made in post #3 in this thread, so there is a good chance that I have guessed wrong in several ways; but, if we rearrange your script slightly, does this come closer to doing what you need? Since you have not told us what shell or operating system you're using, the following code assume that you are using a shell that meets basic POSIX shell command language requirements. And, since we have no sample data to work with, this is entirely untested....

 ERRORDIR=/home/xxxxxxx/ABC/error_directory
 while read -r i
 do  RECIPIENTS="$RECIPIENTS $i@stp.aess.org"
 done < /home/xxxxxxx/ABC/xeenos_scrpts/emailparm
 find /home/xxxxxxx/ABC/logs/*.rpt -mtime -8 ! -name '*ftplog*' | while read -r r1
 do  if grep -q CompletionCode "$r1"
     then
         if ! grep CompletionCode "$r1" | grep -q "CompletionCode: Value=0"
         then
             ( echo " There were errors in the following report file for Xeenos:"
               echo
               echo "This files have been moved to $ERRORDIR"
               echo "$r1"
             ) | mail -s "$r1" $RECIPIENTS
             mv "$r1" "$ERRORDIR"
         fi
     fi
 done

I have strong doubts that the line of code shown in red above is ever going to find a matching line (and will therefore report that every file contains errors; but without seeing the data you are searching, it is just a doubt and I have to assume that those two grep commands will actually find a single line that contains both CompletionCode without a trailing colon and with a trailing colon in report files that indicate successful completion of the associated job.

This will send one e-mail message to your list of recipients for each job that "failed". (If you wanted a single e-mail message summarizing all of the failed jobs found, I assume you would have selected a different subject line for the e-mail messages.)

echo "There were errors in the following report file for Xeenos:" > $EMAILFILE
echo >> $EMAILFILE
echo "This files have been moved to $ERRORDIR" >>$EMAILFILE
 find /home/xxxxxx/ABC/logs/*.rpt -mtime -9 | egrep -v ftplog >$LIST
 while read r1
do
 grep CompletionCode $r1  >/dev/null 2>&1
#grep severity $r1 | grep "Completion:"  >/dev/null 2>&1
   if [ $? = 0 ]; then
      grep CompletionCode $r1 | grep "CompletionCode: Value=0" >/dev/null 2>$1
           if [ $? = 1 ]; then
           echo $r1 >> $EMAILFILE
           echo >>$EMAILFILE
           mv $r1 $ERRORDIR
                for i in `cat /home/xxxxxx/ABC/xeenos_scrpts/emailparm`
                do
                 cat $EMAILFILE | mail -s $r1 $i@der.aes.com
                done
          fi
   fi
done<$LIST

******************
this script works perfect but it keeps adding to the email each file..For instance if 4 files have a rc 4 then an email will go out with the first one then another email will go out with the first and seconf one, then another email will go out with first second and third one listed in the email. I just want one email for each file that has a rc 4.

LIKE THIS:

There were errors in the following report file for Xenos:

This files have been moved to /home/p611568/d2e/error_directory
/home/p611568/d2e/logs/Test_Value_4.rpt
  
 

NOT LIKE:

There were errors in the following report file for Xenos:

This files have been moved to /home/p611568/d2e/error_directory
/home/p611568/d2e/logs/Test_Value_4.rpt

/home/p611568/d2e/logs/bf98009_11_1462885241803.rpt


---------- Post updated at 09:47 AM ---------- Previous update was at 09:27 AM ----------

I am sorry but I am just starting Unix scripting. So with the cript you just sent me don't I need to define the variable RECIPIENTS with every ones name in it so that it will mail to recipients. and when I do that do I actually place their full email address with a comma or a colon?

---------- Post updated at 10:02 AM ---------- Previous update was at 09:47 AM ----------

#!/usr/bin/ksh
#RECIPIENTS=bcarosi
ERRORDIR=/home/p611568/d2e/error_directory
 while read -r i
 do  RECIPIENTS="$RECIPIENTS $i@stryder.aessuccess.org"
 done < /home/p611568/d2e/xeenos_scripts/emailparm
 find /home/p611568/d2e/logs/*.rpt -mtime -10 ! -name '*ftplog*' | while read -r r1
# find /home/p611568/d2e/logs/*.rpt -mtime -8 ! -name '*ftplog*' | while read -r r1
 do  if grep -q CompletionCode "$r1"
     then
         if ! grep CompletionCode "$r1" | grep -q "CompletionCode: Value=0"
         then
             ( echo " There were errors in the following report file for Xenos:"
               echo
               echo "This files have been moved to $ERRORDIR"
               echo "$r1"
             ) | mail -s "$r1" $RECIPIENTS
             mv "$r1" "$ERRORDIR"
         fi
     fi
 done
$

******************
while doing this it just runs in a loop it does not end??????

The mail command you are using is not standardized, but on most UNIX and UNIX-like systems, the mail utility takes one or more operands (after the options have been processed) with each operand specifying the e-mail address of someone who should receive that message. What operating system are you using?

If you want to send mail just to yourself while testing (instead of to the list of recipients specified in /home/p611568/d2e/xeenos_scripts/emailparm ) and to enable us to see where my code is getting stuck in an infinite loop, change the second line of your version of my script from:

#RECIPIENTS=bcarosi

to:

set -xv

and then change the line:

 done < /home/p611568/d2e/xeenos_scripts/emailparm

to the two lines:

 done < /home/p611568/d2e/xeenos_scripts/emailparm
 RECIPIENTS=bcarosi

Move a couple of *.rpt files from /home/p611568/d2e/error_directory back to /home/p611568/d2e/logs so there will be files to process before you run this script again. (I assume that these two directories are on the same filesystem so the timestamps won't change when you move them.)

Then, run the script again and show us the trace log it prints while running (in CODE tags, please) so we can verify that the recipient list is being created correctly and see where my code is going into an infinite loop.

Note that my script moved the code creating the entire mail message inside the then clause of the if statement that sends the email. the code you showed us earlier initializes the mail file contents with three lines of data before entering the for loop and then adds two more lines to that file every time a failed file is found. This is why subsequent mail messages contain more and more files at the end of the message.

1 Like

this script is currently sending 4 emails:
reading:

There were errors in the following report file for Xenos:

This files have been moved to /home/p611568/d2e/error_directory
/home/p611568/d2e/logs/Test_Value_4.rpt

/home/p611568/d2e/logs/bf98009_11_1462885241803.rpt

/home/p611568/d2e/logs/bf98009_12_1462885242812.rpt

/home/p611568/d2e/logs/bf98009_13_1462885243819.rpt

I want the script to send one email for each file without a rc0 like this:

There were errors in the following report file for Xenos:

This files have been moved to /home/p611568/d2e/error_directory
/home/p611568/d2e/logs/Test_Value_4.rpt

currently it send me another one that will say:

There were errors in the following report file for Xenos:

This files have been moved to /home/p611568/d2e/error_directory
/home/p611568/d2e/logs/Test_Value_4.rpt

/home/p611568/d2e/logs/bf98009_11_1462885241803.rpt

then it will send another email with 3 files ....until it reaches the fourth file. I Nedd to have one email send for each file

If you could just tell me what I am doing wront to correct it I would appreciate it

#!/usr/bin/ksh

LIST=/home/p611568/d2e/xenos_scripts/temp/tempfile
ERRORDIR=/home/p611568/d2e/error_directory
EMAILFILE=/home/p611568/d2e/xenos_scripts/temp/emailfile

echo "There were errors in the following report file for Xenos:" > $EMAILFILE
echo >> $EMAILFILE

echo "This files have been moved to $ERRORDIR" >>$EMAILFILE

find /home/p611568/d2e/logs/*.rpt -mtime -10 | egrep -v ftplog >$LIST
(while read r1)
       (do
       grep CompletionCode $r1 >/dev/null 2>&1)CODE]
       (if [ $? = 0 ]; then
        grep CompletionCode $r1 | grep "CompletionCode: Value=0"            
          >/dev/null 2>$1
                (if [ $? = 1 ]; then
                 echo $r1 >> $EMAILFILE
                  echo >>$EMAILFILE
                  mv $r1 $ERRORDIR
                      (for i in                                                   
                         `cat /home/p611568/d2e/xenos_scripts/emailparm`
                        do
                        cat $EMAILFILE | mail -s $r1 $i@sxxxxxr.access.org
          done)
     fi)
fi)
done<$LIST

Let me guess: if there were six or ten files in your directory, you'd receive six or ten emails with up to six or ten files.

I think you've been given the reasons for your script's "misbehaviour" that you complain about in Don Cragun's post#3, and even a solution in post#5. Did you read and understand his proposals?

On top of not applying them to your script, you've introduced several new errors in your last version in post#8, e.g. the many pointless opening and closing parentheses or the pointless redirection to /dev/null.

And, last but not least: Please abstain from opening a new thread for the same topic in order not to scatter the discussion all over the place!

In addition to post #3 that RudiC mentioned, also note the last paragraph of post #7 where I explained exactly why the code I suggested does exactly what you have requested and why your code is doing what it is doing.