If I read this right then it means "every 5 second, open the last scanned page (and nothing else / close the previous one)". But this seems like an inefficient way to do it, opening and killing all irrelevant pages all the time. This will be more efficient and react more quickly:
lastfile=
while true; do
newestfile=$(ls *.pnm | tail -1)
if [ "$newestfile" != "$lastfile" ]; then
kill %
display -rotate 90 "$newestfile" &
lastfile=$newestfile
fi
sleep 0.3
done
Sorry but I find your answer disappointing and crossing over into offending. I spent some time first trying to understand how your code makes sense, then to write up a better solution and posted it, and you don't seem to be thankful at all and are instead dissing my effort. Sure, if it works for you, fine, I was under the impression that you didn't know better. You could have saved me time by indicating that you know your solution is hacky but you don't care.
Yes it is inefficient and weird. And thank you for saving the world. Not only Terran energy crisis, but we are also running out of useful data bits as universal entropy goes to infinity. https://en.wikipedia.org/wiki/Heat_death_of_the_universe
Thanks. Yes, sometimes communication falls short and down, and one's own state of mind is time dependent and doesn't see everything involved. I was in an eager "would like to see positive feedback" state of mind just as timonoko may have been when originally posting their solution, and then interpreted the rejection in an overly negative light. My apologies to them and for the noise.