3

I have a bash script that takes 3 arguments:

$ do_something foo bar baz

and a file with several lines, each with different 3 args.

I want to execute the script as a cronjob, and each time it's called it should call do_something on a line in that file, passing it the three arguments on that line. Next time it's called it should run the next line, so it will take n calls to do_something for it to process n lines. If it processed the last line of the file, it should start over at the beginning next time.

My solution is to remove the processed line from the head of the file each time and append it to tail, but it seems me too cumbersome having all these disk writes just to remember a position. I could have another file around with a pointer to last line, but three files for a simple task is not that smart. Is there a better solution?

  • You are going to have to remember the position anyway. Unless the file so big that rewriting it once every N minutes is an issue, it's a perfectly reasonable method. – Gilles 'SO- stop being evil' Dec 16 '11 at 8:01
2

Well if I have to rewrite the file at least sed will allow me to do it with style:

sed -i '1 { h; d }; $ G' file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.