2

I'm working on bash script that outputs some information every few seconds. Each new output should overwrite the last one (clear the terminal), and when I abort the script no output should be visible when I scroll back in the terminal. Pretty similar to 'watch' (which I cant use here for other reasons).

If I use 'clear', the screen gets cleared but all output is visible when I scroll back in the terminal. 'reset' deletes the entire scrollback, which is also not what I want. Using ANSI sequences like 'printf "\033[2J"' has the same effect as 'clear'.

Any help appreciated! cheers

1

1 Answer 1

3

You could try something like this.

#!/bin/bash

oldrows=$(tput lines)     # get the number of rows
oldcolumns=$(tput cols)   # get the number of columns
count=0                   #set count to 0

control_c()               # Function if CTRL-C is used to exit to set   
{                         #  stty sane again and remove the screen.
tput rmcup                # Removes screen
stty sane                 # Allows you to type
exit                      #Exits script
}


Draw()                           # Draw function, clears screen and echos 
{                                # whatever you want
        ((x++))
        tput clear
        echo  things to screen $x  #x is there simply for this example to show screen changing
}

ChkSize()                       #Checks any changes to the size of screen 
{                               # so you can resize Screen
        rows=$(tput lines)
        columns=$(tput cols)
        if [[ $oldcolumns != $columns || $oldrows != $rows ]];then
                Draw
                oldcolumns=$columns
                oldrows=$rows
        fi
        if [[ $count -eq 10  ]]; then   # Counter 10 is 1 second as loop is every 0.1 
                Draw                    # seconds. Draw and sets count to 0
                count=0
        fi


}
main()
{
stty -icanon time 0 min 0       # Sets read time to nothing and prevents output on screen
tput smcup                      # Saves current position of of cursor, and opens new screen    
Draw                            # Draw function
count=0                         #Sets count to 0
keypress=''                          #Sets Keypress to nothing
while [ "$keypress" != "q" ]; do      #Loop will end when q is pressed,can change to whatever condition you want
        rows=$(tput lines)           #Sets rows and cols
        columns=$(tput cols)
        sleep 0.1                    #To prevent it from destroying cpu
        read keypress                #Reads one character
        (( count = count + 1))      #Increment the counter
        ChkSize                     #Runs the checksize function
        trap control_c SIGINT  #Traps CTRL-C for the function.
done

stty sane                              #Reverts stty
tput rmcup                               #Removes screen and sets cursor back"     
exit 0                           #Exits
}

main #Runs main

Hope this helps :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.