I'm using dwm with the dualstatus patch. This adds a status bar on the bottom of the screen, in addition to the standard bar at the top. The text in each bar is set in ~/.xinitrc (for example) like this:

xsetroot -name “top text;bottom text”

Is there a way in bash to set the top text and bottom text at different intervals? For example, I have a script topbar that displays system information, e.g. output from the uptime command, and a script bottombar that displays information like weather, battery state, etc.

The goal is to have the top bar update every second, while the bottom bar only updates every minute because its information comes from more expensive processes (e.g. querying my music player, checking the battery state, etc.) Right now my ~/.xinitrc looks like this:

while true; do
    for s in {1..60}
        xsetroot -name "$(topbar);$bottomdisp";
    sleep 1;
done &

( ( sleep 5 && /usr/bin/xscreensaver -no-splash -display :0.0 ) & )

exec rundwm

This updates every second, though. Is there a simpler way to do this? The ideas I could think of were

  1. Maybe a way to tell xsetroot to preserve whatever's in the bottom bar? As a last resort, I may tweak some of the code in the dualstatus patch to allow it to preserve the current state of the top/bottom bars if something like xsetroot -name ';bottom text' is passed, but that's not ideal because my C is rusty and I still use the above command to clear the bars at times.

  2. Use a cronjob to update a cache of the text in the bottom bar, and run that once a minute. Even though the top/bottom bars would display every second, only the top bar would actually change every second.

Any other methods for this? Is there a simple(r) way to do this in bash that I missed?


I would suggest having bash keep track of your previous bottom string and only update it once a minute (when seconds of the current time modulo 60 is equal to 0 in this code).

while true; do 
    (( 10#$(date +%s) % 60 )) || bottomdisp=$(date)
    xsetroot -name "$(topbar);$bottomdisp";
    sleep 1;
done  &

This syntax makes it easy to modify the frequency of the secondary (or have multiple) intervals, e.g. just change 60 to 15 for updates 4 times a minute.

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.