Find the answer to your Linux question:
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just 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

  2. #2
    Just Joined! barriehie's Avatar
    Join Date
    Apr 2008
    Location
    The Desert!
    Posts
    85
    Is this the only text in the file, and the only line?
    Code:
    Warning: Server 192.168.0.1 is not reachable

  3. #3
    Just 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

  4. #4
    Just Joined! barriehie's Avatar
    Join Date
    Apr 2008
    Location
    The Desert!
    Posts
    85
    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

  5. #5
    Just Joined!
    Join Date
    Nov 2010
    Posts
    7
    Thanks a lot barriehie! I will try it and i will get back

  6. #6
    Just 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

  7. #7
    Just Joined! barriehie's Avatar
    Join Date
    Apr 2008
    Location
    The Desert!
    Posts
    85
    Quote Originally Posted by timvte View Post
    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
    Don't understand about more than one file but I'm glad it works!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •