0

I have a select query which runs and generate the count. It exports the Answer in a text file which has below records eg.:

Unprocessed record for A Sources
----------------------Unprocessed record count for ABC SOURCE ------------------
abc_count=            0
--------------------Unprocessed record count for CDE Source --------
CDE_count=            0
-----------------------Unprocessed record count for DEF Source -----------------
DEF_count=            0
------------------Unprocessed record count for GHI Source ----------------------
GHI_count=            56

I want to write a Unix command which checks if for above source any of the count is not equal to 0 then mail it to the required team.

This is what I used previously but is not working.

for CHECK in $(awk '{print $1}' hello | grep count=| cut -d"=" -f1) do
    if [ $(grep $CHECK output.txt | awk '{print $2}') -ne 0 ] then
        echo "$(grep $CHECK output.txt)" | mailx -s "CHECK ABC PROCESS" foo@bar.com
    fi
done
  • This is what i used previously but is not working for CHECK in $(awk '{print $1}' hello | grep count=| cut -d"=" -f1) do if [ $(grep $CHECK output.txt | awk '{print $2}') -ne 0 ] then echo "$(grep $CHECK output.txt)" | mailx -s "CHECK ABC PROCESS" foo@bar.com fi done – Vinayak Das Sep 9 '17 at 9:20
  • please help me with any thing new or let me know if above one has any sytactical error – Vinayak Das Sep 9 '17 at 9:22
0

You should add a semicolon ; in if statement before then and in for before do. If no more issues it should work. Also I'm not improving your script.

The script.

for CHECK in $(awk '{print $1}' hello | grep count=| cut -d"=" -f1); do
    if [ $(grep $CHECK output.txt | awk '{print $2}') -ne 0 ]; then
        echo "$(grep $CHECK output.txt)" | mailx -s "CHECK ABC PROCESS" foo@bar.com
    fi
done

Also it's not you should have your cut command as this cut -d"=" -f2 to get number value? also better run your commands out of loop to see if you got any result or not.

0

To send an email to foo@bar.com if the file contains an assignment of anything non-zero to anything:

  1. Filter out all lines with assignments. This may be done with grep '=' (alternatively `grep 'count=' if you need to be more specific).
  2. Remove lines with lone zeroes. This may be done with grep -v -w '0' (-w to match only complete words, i.e. not 10 in this case).
  3. Act on the result.
if grep '=' output.txt | grep -q -v -w '0'; then
    grep '=' output.txt | grep -v -w '0' | mailx -s "Check this out!" foo@bar.com
fi

For the data in the question, that will send an email containing

GHI_count=            56

If it feels unnecessary to run four greps for this:

tmpfile=$(mktemp)
grep '=' output.txt | grep -v -w '0' >"$tmpfile"

if [ -s "$tmpfile" ]; then
    mailx -s "Check this out!" foo@bar.com <"$tmpfile"
fi

rm -f "$tmpfile"

The -s test will be true if the file exists and has a size greater than zero.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.