Help with script logic.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with script logic.
# 1  
Old 08-26-2011
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)

Code:
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
# 2  
Old 08-26-2011
Code:
if grep ABENDED filename >/dev/null && [[ ! -f flagfile ]]
then
     mail -s subject user@example.com <<EOF
mailbody
EOF
    touch flagfile
fi

# 3  
Old 08-26-2011
try this .. Initially put "0" in tempfile (for first time run only)
Code:
#!/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


Last edited by jayan_jay; 08-26-2011 at 06:47 AM..
# 4  
Old 08-26-2011
Quote:
Originally Posted by jayan_jay
temp_val=`awk '{print $1}' tempfile`
current_val=`grep -n ABENDED inputfile | tail -1 | awk -F: '{print $1}'`
i am not good at awk :-) can youplease explain ?
# 5  
Old 08-26-2011
Code:
 
current_val=`grep -n ABENDED inputfile | tail -1 | awk -F: '{print $1}'` 
to
current_val=$( awk '/ABENDED/ {val=NR} END{print val}' inputfile )

Code:
 
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

Login or Register to Ask a Question

Previous Thread | Next Thread

2 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need script logic

i have two csv files (rates.csv, reference.csv) as below. rates.csv contains Code, respective Zone details rates.csv ---------- 23,38Nhava 45,37NEWYORK 89,SHILANG 71,ROBACCA reference.csv contains all Zone details reference.csv ------------- 37Newyork robacca 38Nhava... (5 Replies)
Discussion started by: p_satyambabu
5 Replies

2. Shell Programming and Scripting

Script logic

Please help me to write the following script. 1) Input files will be in Test directory. (Test/YYYY/MM) folders (Ex: 1) 2010/ 01 2) 2011/05) The file name will be Testfile1_6676543218_Axxxxxxyyyyyzzzzzzzzzzzzzzzzzz.pdf.gz 2) The compare file will be in /tmp directory. (/tmp/comparefile.txt). The... (6 Replies)
Discussion started by: mnjx
6 Replies
Login or Register to Ask a Question