bash loop with GNU less that refreshes every 2 min

2019-07-11 10:16发布

I have a linux machine with a data folder and a script that produces a report xlsx spreadsheet file on the ever changing content of the folder where it is being run. I use a xlsx2tsv script to convert it to tsv text which takes less than one second. The script takes about 1 minute to produce the spreadsheet file and I want to have a terminal screen showing the results of the table in a GNU less buffer, where I can move around with the cursors, and search for content with /search, etc. of the most up-to-date version of the content.

At the moment I have a bash while true; do loop which first calculates the content of the script, then transforms it to tsv and pipes it to GNU less. Then in another terminal screen I have a while true; do loop that kills the less command every 2 minutes. But this leaves me with 1 minute of inactivity, where I am waiting for the spreadsheet to be updated before being able to navigate the content with less.

I would like to optimise this setup so that a new spreadsheet is produced in the background not when I kill the less command, but starting 1 minute before, so that the updated less command always gives me a working copy that I can navigate. Ideally without having to create a third terminal window for it.

Any ideas? Maybe GNU parallel?

Current setup:

# screen in the data folder with less command
while true; do $HOME/script -dir $PWD && xlsx2tsv $(ls $PWD/*.xlsx) 1 | column -t | less -S -N; sleep 0.5; done
# screen 2 with the less killing
while true; do pkill less; sleep 120; done

2条回答
萌系小妹纸
2楼-- · 2019-07-11 10:52

My less man page says this about the "R" command

R Repaint the screen, discarding any buffered input. Useful if the file is changing while it is being viewed.

So, just hit R to refresh.

查看更多
可以哭但决不认输i
3楼-- · 2019-07-11 10:58
while true; do
  (sleep 0.5
   $HOME/script -dir $PWD &&
   xlsx2tsv $(ls $PWD/*.xlsx) 1 |
   column -t > new;
   mv new old;
   killall less
  ) &
  less -S -N old
done
查看更多
登录 后发表回答