How to refresh a graphical display through bash script

Hi folks ,

I need to display a message graphically using a messagebox or textbox through bash script. However the message should be keep changing every 4 secs . I input the message from a file and use "gxmessage" to display it .

gxmessage -nofocus -center -title "Welcome screen" -geometry "600,600" -wrap -file $random

Where "$random" is some random data from a file.
But the problem is , how should I refresh the same gxmessage message box,putting different message each time .

I thought of

while :
do
        sleep 4
        if ps | grep "gxmessage"
        then
                killall -9 "gxmessage"
        fi
        gxmessage -nofocus -center -title "Welcome screen" -geometry "600,600" -wrap -file $random &
 done &

I hate this solution and it looks ugly too , because it is visible that the message box is getting killed and forked every time .

Can't I use the same gxmessage message box for all the messages just by putting new messages and hence refreshing the same box.
Any idea , anything other than gxmessage will do , if a solution is somewhere else.

And I am a newbie :wall:

Thanks.
D.

Take a look at: Bash Prompt HOWTO: Chapter 6. ANSI Escape Sequences: Colours and Cursor Movement

If you're not dead set on using gxmessage, you could start an xterm and execute a small script which pulls random data from a file. Here's a sample that computes a random line to pull from a cookie file and displays it for a few seconds before pulling and displaying the next. If you're willing to write the script given to xterm as a standalone, then the xterm command is less of a mess.

xterm +sb -T "window title" -geometry 132x24+100+100 -foreground yellow -background blue -e 'w=10 
cookie_file=~/lib/biscuits   # one biscuit (or American cookie) per line
n=30    # number of lines in ibiscuits
while (( $w > 0 ))
do
    r=$(( ($RANDOM % $n) + 1))
    cookie=$( head -$r $cookie_file | tail -1)
    print "\0033[H$(date)  $r $cookie\0033[K"  # home cursor, write cookie, clear to end of line
    sleep 5
    w=$(( w - 1 ))
done'
1 Like

Thanks all.
@agama : I did this using xterm and found that really useful related to my requirement , for refreshing , I just cleared the screen every 2 seconds .

xterm -geometry 20x3+1300+0 -ms "green"  -fs +80 -T "STAT" -w 4  -bg "black" -fg "Gold" -e ./cpuscript.sh &

Where the script called is an infinte loop to hold,display,and clear the screen.

#!/bin/bash
############# SCRIPT CALLED BY XTERM TO DISPLAU CPU AND MEMORY USAGE ######################
while :
do
        clear
        echo "CPU: $(cat $cpustat | tail -1) %" #File to get dynamic CPU usage , have done it using ps in another script
        echo "MEM: $(cat $memstat | tail -1) %" #File to get dynamic Mem usage , have done it using ps in another script
        sleep 2
done

exit 0

Thanks once again.