Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
    
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 Dec 16 '11 at 8:01
add comment

1 Answer

up vote 2 down vote accepted

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
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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