I have a command that is continually adding IPs (do not know the quantity) to a txt file called ips.txt
shodan stream --alert=all --datadir=. --compresslevel=0 >> ips.txt
I want to get the newly added data to the ips.txt file every 1 hour and make some bash operations on it as I will leave this command running in the background.
How can I get only the newly added data to this file every time automatically?
The
shodan()function is to mimic what yourshodancommand is doing and thetrapis to kill theshodan()child process on exit so we can test the main part of the code, the final loop.I'm using
wcspecifically as it counts newlines in it's input so if somehowshodancould have written a partial line at the end of ips.txt it won't get picked up until the next loop iteration when it's a complete line. That's also why I'm piping thetailoutput to a subsequenthead, to ensure we just get those complete lines. I'm also usingtail | headinstead of, say, a singleawkcommand as tail reads from the end of the file and so will be more efficient for this than awk which always starts at the beginning.The
if (( endWc < begWc ))block is to start printing from the first line again if the file shrunk - that may or may not be what you want to do in that situation, idk. You'll also want to add a bit of code to do whatever you want to do if the files is empty (if (( endWc > 0 ))fails) on any iteration and think about whether or not there are any other rainy-day scenarios to cover.I interrupt
./tst.shto stop it, and thecat ips.txthas a couple of extra lines asshodan()runs once per second so the main "every 3 seconds" loop (obviously change that tosleep 3600or do whatever else you like to sleep for an hour) hasn't processed all the lines at that point. The important thing is that no lines are missing from the output prior to that.