Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am trying to launch GNU Screen using my .bashrc. I'm almost there:

if [ -z "$STY" ]; then
   exec screen -dR
else
   exec gnome-terminal
fi

This is wrong through! The first case works, screen launches when I open a terminal. The second part fails though. I want to open a regular terminal if I already have one open. But this just opens an infinite number of terminals...

share|improve this question

3 Answers 3

You could try (after the else):

if [ "$HAS_STARTED_TERM"!="1"]; then
    HAS_STARTED_TERM=1
    export HAS_STARTED_TERM
    exec gnome-terminal
fi
share|improve this answer

If I'm not mistaken, the terminal would open normally if you just remove the "else" block... Or maybe I'm missing something.

share|improve this answer
1  
Well, the gnome-terminal probably hosts another copy of bash, which itself runs the .bashrc, and which in turn opens another gnome-terminal again... in an infinity of loops. –  scraimer Apr 30 '09 at 20:51
    
Yes, that's my thinking, but it's not completely clear to me what devin is trying to accomplish. –  Ivan Apr 30 '09 at 20:55

The way I have things set up I launch screen on every server I connect to if it has screen. When I disconnect and reconnect, the same screen session is reconnected. Sounds good so far, that's what everyone wants. But, if you have several windows open in screen, how do you disconnect without closing them? There are 2 options:

  1. My preferred ssh client will disconnect if I hit "`." and in my screenrc I use "autodetach", but that only works remotely.
  2. For local (or remote) screens I have my bashrc set to disconnect if I detach from screen. However, in rare cases you want to detach and stay connected. For that, I have some magic in my .bash_logout to complement my .bashrc magic.

.screenrc

hardstatus alwayslastline
hardstatus string '%{= kG} %{G}%H %{g}[%= %{=kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}] %{W}%c %{g}'
#dynamic title
shelltitle '# |sh'

#When your ssh connection dies, screen is autodetached
autodetach on 

#terminfo and termcap for nice 256 color terminal
# allow bold colors - necessary for some reason
attrcolor b ".I"
# tell screen how to set colors. AB = background, AF=foreground
termcapinfo xterm "Co#256:AB=\E[48;5;%dm:AF=\E[38;5;%dm"
termcapinfo xterm-color "Co#256:AB=\E[48;5;%dm:AF=\E[38;5;%dm"
# erase background with current bg color
defbce "on"

.bashrc

# set a fancy prompt (non-color, unless we know we "want" color)
if [[ $TERM =~ xterm-.*color || $TERM =~ screen.* ]]; then
   PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]# '
   #if [[ $TERM =~ screen.* ]]; then
   export SCREEN_CMD=$(which screen 2>/dev/null)
   if [[ ( $TERM =~ screen.* ) || ${SCREEN_CMD-X} != X && ${SCREEN_CMD-X} != "" ]]; then
      # This is the escape sequence ESC k \w ESC \
      #Use path as title
      #SCREENTITLE='\[\ek\w\e\\\]'
      #Use program name as title
      SCREENTITLE='\[\ek\e\\\]'
   else
      #Soliton@freenode#screen suggested screen -xRRS primary
      echo ^[k$(hostname|sed "s/\..*//")^[\\
      export SCREEN_CMD=$(which screen 2>/dev/null)
      if [[ ${SCREEN_CMD-X} != X && ${SCREEN_CMD-X} != "" ]]; then
         screen -xRRS primary && unset SCREEN_CMD && [[ $(stat -c %Y .screen_do_not_disconnect 2>/dev/null || stat -f %m .screen_do_not_disconnect 2>/dev/null) -gt 0 ]] || exit
      fi
   fi
else
   PS1='\u@\h:\w# '
   SCREENTITLE=''
   #Soliton@freenode#screen suggested screen -xRRS primary
   echo ^[k$(hostname|sed "s/\..*//")^[\\
   export SCREEN_CMD=$(which screen 2>/dev/null)
   if [[ ${SCREEN_CMD-X} != X ]]; then
      screen -xRRS primary && unset SCREEN_CMD && [[ $(stat -c %Y .screen_do_not_disconnect 2>/dev/null || stat -f %m .screen_do_not_disconnect 2>/dev/null) -gt 0 ]] || exit
   fi
fi
PS1="${SCREENTITLE}${PS1}"

.bash_logout

# ~/.bash_logout

time="$(TZ=UTC date -d @0 2>/dev/null||date -r 0 +%Y%m%d%H%M)"
touch -d "$time" ~/.screen_do_not_disconnect 2>/dev/null || touch -t "$time" ~/.screen_do_not_disconnect

The scripts above work on both Linux and Mac OS X (which is the reason you see "2>/dev/null ||" in the stat, touch, and date commands). I also use a 256 color xterm and I believe all the supporting code is included here. Despite the length of this post, I have trim a lot out of the scripts/configs.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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