I have a shared home directory that is automounted via NFS on all the UNIX servers I log in to. I would like to have a single .screenrc that (for example) puts the hostname in the hardstatus line in a different colour to indicate when I am logged on to production versus development; our hostnames are such that this can easily be done through the use of pattern matching.

Is there any way of putting conditional statements into .screenrc? A quick grep of the man page doesn't seem to reveal anything obvious.

EDIT:

To clarify, I already have a custom hard status line; I want to change some of the colouring/fields displayed based on the hostname that screen is invoked on. I already have something like this for my PS1, however I would rather do this in the screen status line as I jump off from these hosts to routers/switches, which don't allow me to do any prompt colourisation. This way, I can have one window for production, and one for development, with the colour of the screen status line telling me which one I am on at a glance.

link|improve this question

Hostname of the machine that screen is running on? Or the host name of that particular screen 'window' is using? – Kyle Brandt May 29 '09 at 11:35
@Kyle: hostname of the machine that screen is running on. See the updated question for a bit more detail. – Murali Suriar May 29 '09 at 12:34
feedback

migrated from serverfault.com Jul 10 '11 at 23:39

This question came from our site for system administrators and desktop support professionals.

3 Answers

up vote 7 down vote accepted

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.

link|improve this answer
When you read ".screenrc_hostname" read ".screenrc_`hostname`". I got problem to include ` in the post – radius Jun 30 '09 at 0:08
Nice - I missed the part in the man page about the SCREENRC environment variable being searched first. – Murali Suriar Jun 30 '09 at 11:17
feedback

@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|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
# The next line is problematic on some of my systems, so I have it commented out.
#GT_UBAR=$(    tput set smul unset rmul) # smul unset rmul :?: Set “underscore” (underlined text) attribute
GT_BLINK=$(   tput blink) # Set “blink” attribute
GT_REVERSE=$( tput rev)   # Set “reverse” 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
FG_DEFAULT=$( tput setaf 9) #default foreground color

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

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

link|improve this answer
feedback

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.

link|improve this answer
That doesn't allow me to conditionally format the hostname based on its structure (e.g. any hostname ending in '-dev' in blue, any hostname ending in '-prod' in red.) – Murali Suriar Sep 27 '11 at 17:54
ah. if only screenrc if-statements supported regex. – Tim Kennedy Sep 27 '11 at 20:47
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.