Help with script logic.

Hello people,

I need some help with the logic of my script.

I need to have a process (mailx alert) scheduled frequnetly
My requirement is to grep a specific file for a specific word.
If the word is found send me an alert.
However if the word was also found the previous run do not send the mail(kind of reset mechanism for the alert)

GREP FILE for the word ABENDED
IF word ABENDED found
THEN
    IF FLAG EXIST
    THEN EXIT
    ELSE MAIL USER, TOUCH FLAG
ELSE EXIT

My main idea is above however not completed.
Can you please provide sample code?

Thank you in advance

if grep ABENDED filename >/dev/null && [[ ! -f flagfile ]]
then
     mail -s subject user@example.com <<EOF
mailbody
EOF
    touch flagfile
fi

try this .. Initially put "0" in tempfile (for first time run only)

#!/bin/bash
temp_val=`awk '{print $1}' tempfile`
current_val=`grep -n ABENDED inputfile | tail -1 | awk -F: '{print $1}'`
if [ "$current_val" -gt "$temp_val" ]; then echo "Mail syntax"; else echo "Alert sent already"; fi
echo "$current_val" > tempfile

i am not good at awk :slight_smile: can youplease explain ?

 
current_val=`grep -n ABENDED inputfile | tail -1 | awk -F: '{print $1}'` 
to
current_val=$( awk '/ABENDED/ {val=NR} END{print val}' inputfile )
 
bash-3.00$ cat test
a
b
c
d
e
f
g
h
bash-3.00$ nawk '/c/{val=NR} END{print val}' test
3
bash-3.00$ echo "c" >> test
bash-3.00$ !nawk
nawk '/c/{val=NR} END{print val}' test
9
bash-3.00$ cat -n test
     1  a
     2  b
     3  c
     4  d
     5  e
     6  f
     7  g
     8  h
     9  c