How can you change the GNU screen status line based on hostname?

@radius is spot-on with the SCREENRC thing, but it wasn't a very complete answer, so I'll elaborate…

~/.bashrc

# Strip down a FQDN
hostname="$(hostname | sed 's/\..*//')"
# Use the case pattern for server groups
case "$hostname" in
    mario|luigi|toad|peach|koopa*|bowser) export SCREENRC=~/.screenrc_prod;;
    dev*|vm*)  export SCREENRC=~/.screenrc_dev;;
esac
# Use condensed bash "new test*" notation to override for specific servers
# e.g. ~/.screenrc_bowser would get used instead of ~/.screenrc_prod
[[ -f "~/.screenrc_$hostname" ]] && export SCREENRC="~/.screenrc_$hostname"

* Always use new tests!

A word about escape characters

The proper way to include colors in scripts is with tput, not escape characters. Your escape characters are specific to your terminal. The tput command is terminal aware. I wrote this for when I need to get fancy in my scripts:

~/bin/COLORS.sh

GT_RESET=$(   tput sgr0)  # Reset all attributes
GT_BRIGHT=$(  tput bold)  # Set “bright” attribute
GT_DIM=$(     tput dim)   # Set “dim” attribute (normal/non-bright)
GT_ULINE=$(   tput smul)  # Set “underscore” (underlined text) attribute
GT_BLINK=$(   tput blink) # Set “blink” attribute
GT_INVERSE=$( tput rev)   # Set “inverse” attribute
GT_HIDDEN=$(  tput invis) # Set “hidden” attribute

FG_BLACK=$(   tput setaf 0) #foreground to color #0 - black
FG_RED=$(     tput setaf 1) #foreground to color #1 - red
FG_GREEN=$(   tput setaf 2) #foreground to color #2 - green
FG_YELLOW=$(  tput setaf 3) #foreground to color #3 - yellow
FG_BLUE=$(    tput setaf 4) #foreground to color #4 - blue
FG_MAGENTA=$( tput setaf 5) #foreground to color #5 - magenta
FG_CYAN=$(    tput setaf 6) #foreground to color #6 - cyan
FG_WHITE=$(   tput setaf 7) #foreground to color #7 - white

BG_BLACK=$(   tput setab 0) #background to color #0 - black
BG_RED=$(     tput setab 1) #background to color #1 - red
BG_GREEN=$(   tput setab 2) #background to color #2 - green
BG_YELLOW=$(  tput setab 3) #background to color #3 - yellow
BG_BLUE=$(    tput setab 4) #background to color #4 - blue
BG_MAGENTA=$( tput setab 5) #background to color #5 - magenta
BG_CYAN=$(    tput setab 6) #background to color #6 - cyan
BG_WHITE=$(   tput setab 7) #background to color #7 - white

I also made a script to demonstrate the use of tput. You can get it from this gist.

See:
http://www.ibm.com/developerworks/aix/library/au-learningtput/?S_TACT=105AGY06
http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x405.html


I see two way to do this, the first one is to make a .screenrc file by host.
Like .screenrc_serverA, .screenrc_serverB, ...
In your shell startup script set SCREENRC to something like .screenrc_`hostname`
Of course you can use the source command of screen to include something like .screenrc_default in each custom .screenrc_… files so that they only contains a caption/hardstatus line and not the whole configuration each time.

The second way would be to execute commands like screen -X hardstatus lastline ... (using if tests to execute the command with different value for ... depending of the hostname) in your shell startup script. When you will log on the server, screen -X will do nothing because screen will not yet be launched, but each time you open a new windows in screen the hardstatus will be updated.

Of course the 1st solution is better because the second one will refresh the hardstatus line each time you opened a news windows which is probably useless as the hostname will not have changed.


Just set it in your .screenrc as the hardstatus variable. As long as you're using a variable like %H for the hostname, it will change to match any hostname you work on it.

hardstatus alwayslastline "%{rk}%H %{gk}%c %{yk}%M%d %{wk}%?%-Lw%?%{bw}%n*%f %t%?(%u)%?%{wk}%?%+Lw%?"

Will give you a status line at the bottom that looks like

screen hardstatus example

hostname is red on black, time is green on black, date is yellow on black. window marked with * is the current window, shown in blue text on white background. window marked with - is the previously active window, shown white text on black. window 1, above, is neither current, or previous, and would be shown in white on black. the other possibilities would show you other users attached to windows, white on black, and be marked by +.

having the time display also helps with keep alives if you ssh to remote servers to run screen, as the time changing every minute will keep your session alive, even if the shell is idle on the remote side.