Manipulate condition to send mail based on output text in file

Hi All,

I have a working script as below.

echo "Files loaded with $(cat /var/tmp/script.X1.out)" | mail -s "Files loaded with return code" mailid 

This script takes the output from script.X1.out file and appends the text "Files loaded with return code" and sends the email. Now what I want is to append Status as Success or Failure based on return code in the .out file. like as below.

if script.X1.out contains, Return code 0, then SUCCESS to be appended in the email.
if script.X1.out contains, Return code 2, then FAIL to be appended in the email.

Please help me build this piece of code. Kindly suggest .
Thanks,
Midhun

By "append" your mean change the subject line - I think.
Let's tidy up the "script":

# Assume failure
code="FAIL"
# silently check the input file, if okay change the code
grep -q  'Return code 0' /var/tmp/script.X1.out && code="SUCCESS"
#email with correct code
echo "Files loaded with $(cat /var/tmp/script.X1.out)" | mail -s "Files loaded with return code $CODE" mailid

Note: mailid is a variable - I guess - so it needs a '$' in front of it in that case
Next time you post a question please tell us your OS and shell.

2 Likes

HI Jim,

Thanks for your answer. I meant append is referring to content in the email. i.e I have to extract only from the .out file the return code of a ABC.sh script. that I'm handling in another code.
So basically,
"echo "Files loaded with $(cat /var/tmp/script.X1.out)" | mail -s "Files loaded with return code" mailid " -- this code gives the output(return code) from file and send email with subject in quotes.

All I want is

  • if script.X1.out contains, Return code 0, then SUCCESS to be appended after the first pipeline in the email content.
  • if script.X1.out contains, Return code 1, then WARNING to be appended after the first pipeline in the email content.
  • if script.X1.out contains, Return code 2, then FAIL to be appended after the first pipeline in the email content.

script.X1.out value always changes so i'm doing cat from it or i can change it to grep as you suggested. can you please help on the code for the above scenario.

OS : Linux.

Thanks,
Midhun

Not sure I get what you want. How far would this get you (assuming your shell, which you fail to mention, being bash ):

RT=(SUCCESS WARNING FAILURE)
echo "Files loaded with ${RT[$(grep -i 'Return code' /var/tmp/script.X1.out) | grep -o [0-9]*)]}"
2 Likes

Hi RudiC,
Can you please explain the above code what you replied. How does the above code fetch Return code from file and assign SUCCESS FAIL WARNING based on it ?

The first grep retrieves the line "Return Code n", the second extracts the number n which becomes the result of the "command substitution" which in turn serves as an index into the RT array:
0 ->Success
2 -> Failure

Moderator comments were removed during original forum migration.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.