Results 1 to 7 of 7
Hello,
I need some help from you guys…
As a general idea, I need to make a bash script that should run each minute. Inside a file I have a ...
- 11-16-2010 #1Just Joined!
- Join Date
- Nov 2010
- Posts
- 7
need help for a bash script
Hello,
I need some help from you guys…
As a general idea, I need to make a bash script that should run each minute. Inside a file I have a text like: “Warning: Server 192.168.0.1 is not reachable”. The file will be deleted and another file with another text (with the same format) will be written but sometimes with a different IP (ie “Warning: Server 192.168.0.3 is not reachable”). The script should look for this text inside the file. If the part of the text (the IP in this case) is the same from the last check then do nothing. But if the IP is not the same, then do whatever…
I am thinking to take the text and place it in a temp/log file and the script should check if the last line is the same as the previous one… But I am not sure if this is the best way to do it or how to do it…
Any idea, link or help will be greatly appreciated.
PS I’m a beginner in bash scripting so do not throw with rocks
- 11-17-2010 #2
Is this the only text in the file, and the only line?
Code:Warning: Server 192.168.0.1 is not reachable
- 11-17-2010 #3Just Joined!
- Join Date
- Nov 2010
- Posts
- 7
hi barriehie,
No, this is not the only text from the file. This is like the head of the whole file.
Meanwhile I made a line that is looking for the line and place it in a log file.
Something like:
cat /root/Dir/* | awk '!/Warning:/' >> /root/Dir2/log.log
where Dir is the directory where is the text file/s.
Then I will try to find a way to compare the last 2 lines… and then… I’ll see
- 11-18-2010 #4
Okay, thinking way back to my bbs'ing days something like this:
Code:#!/bin/bash if [ -e /root/Dir2/flagfile ] # flagfile indicates script has run before. then mv /root/Dir2/log.log /root/Dir2/log-last.log cat /root/Dir/* | awk '!/Warning:/' >> /root/Dir2/log.log diff /root/Dir2/log.log /root/Dir2/log-last.log > /root/Dir2/oops oopsflag=$(ls -l /root/Dir2/oops | gawk '{ print $5 }') if [ $oopsflag -ne 0 ] then # # Do whatever when the files are different. # else # # Do whatever when the files are the same. fi else # No flagfile, this is the first run. cat /root/Dir/* | awk '!/Warning:/' >> /root/Dir2/log.log touch /root/Dir2/flagfile fi exit 0
- 11-18-2010 #5Just Joined!
- Join Date
- Nov 2010
- Posts
- 7
Thanks a lot barriehie! I will try it and i will get back
- 11-18-2010 #6Just Joined!
- Join Date
- Nov 2010
- Posts
- 7
I tested the script and it's working as it should
There is a problem if i have more than one file in the DIR (even the text is the same or not). I've noticed some wired outputs
But I will try to find a way to avoid this...
Thank you very much for your time and help
- 11-22-2010 #7


Reply With Quote
