0

Tried to figure out but could not find much help. I want to capture the count of the table and send an email if the count is more than 0. The output in the email should be as shown below only if it crosses the count more than 0. Please help, thanks in advance for helping me out.


spool log;

SELECT count(*)
FROM users
;

spool off

EOF


count=`grep -rn  log | wc -l`
if [ $count != "1" ]
then
cat log | mailx -r [email protected] " ${MAILTO} > /dev/null 2>&1
else
echo "No Sessions found"
fi

Output:

Count(*)
--------
      2
0

1 Answer 1

0

To simplify the work you can replace:

count=`grep -rn  log | wc -l`
if [ $count != "1" ]

with

count=`tail -1  log`
if ["$count" -gt 0 ]

the sql will return only one value (this is the idea of count(*) command) so do not need of wc -l, just grab the last line. And use -eq when compare numbers in bash

7
  • Thanks for the help @Romeo but it is not comparing and says no sessions found. Below is the output. ++ tail -2 log + count=' 14' + '[' 14 -eq 1 ']' + echo 'No Sessions found' No Sessions found Commented Feb 28, 2022 at 9:42
  • @James, my mistake, I edit the answer, should be -ne, not -eq. ANd use -1 in tail, -2 will get two lines. If you have the number one line before the last use tail -2 log|head -1 Commented Feb 28, 2022 at 9:54
  • @Roman It worked but not as expected. It should send an alert when the count is greater than 0 and the alert should not be triggered when the count is 0. Please help me in this. Commented Feb 28, 2022 at 10:02
  • @James, check my edited answer Commented Feb 28, 2022 at 10:04
  • 1
    Apologize for addressing you incorrectly. Thank you so much for your help. Commented Mar 1, 2022 at 10:31

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.