I'm writing a script in which I pass the output of a function to less, wait for some time, and then kill less. All of this done inside a loop. This makes it look like less is updated on the fly. This is my script:

#!/usr/bin/env bash

for i in {0..5}; do
    echo $i | less -c &
    sleep 2
    kill $pid &>/dev/null

This works as expected, but the terminal kind of becomes unresponsive (keystrokes are not visible for instance) after this. What am I doing wrong?

EDIT: Okay, I think the way I asked the question was confusing. I'm trying to write a script which scrapes lyrics off a website according to the current song in music player. The way I want to do this, I'll keep this script running in a screen session, and I want it to auto-update every 30s or so. The finall script would look something like:

function get_lyrics()
    # function which gets the lyrics based on current song

while true; do
    echo "$song" | less -c &
    sleep 30
    kill $pid &>/dev/null
  • Why are you send less to foreground? In my opinion here is a very strange script – dchirikov Jan 10 '13 at 18:59
  • @dchirikov Foreground? less & sends it to the background. – metwarp Jan 10 '13 at 19:03
  • Not sure exactly what you are trying to accomplish, but you realize that less already has a tail mode that refreshes the end of the file? less -F (or invoke with F if already in a less session). – George M Jan 10 '13 at 19:06
  • @metwarp Yes. Background of course. – dchirikov Jan 10 '13 at 19:08

Create a script file and put the following in there. It will write lyrics on a file /tmp/lyrics

while true; do
    get_lyrics >/tmp/lyrics
    sleep 30

Now, send that script in the background and issue the following, which will do what you are trying to do, but in a cleaner way.

watch -n 30 less -c /tmp/lyrics
  • This has a problem - using watch takes out scrolling functionality from less. – metwarp Jan 10 '13 at 20:51

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.