2

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 though! The first case works, screen launches when I open a terminal. But the second part fails. I want to open a regular terminal if I already have one open. But this just opens an infinite number of terminals...

janos
  • 808
  • 1
  • 6
  • 22
devin
  • 1,246
  • 3
  • 20
  • 27

4 Answers4

2

You could try (after the else):

if [ "$HAS_STARTED_TERM"!="1"]; then
    HAS_STARTED_TERM=1
    export HAS_STARTED_TERM
    exec gnome-terminal
fi
Lucas Jones
  • 389
  • 2
  • 5
1

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

Ivan
  • 3,172
  • 4
  • 25
  • 34
  • 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. – Shalom Craimer 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
1

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.

Bruno Bronosky
  • 4,529
  • 3
  • 26
  • 34
0

Just type:

if [ -z "$STY" ]; then
   screen -ARR
fi

Notice that there is no exec and that else is not needed. You can then simply type exit to get back to a regular terminal.

Using -ARR has the added benefit that it will always resume the first appropriate detached screen session, if there is one. Otherwise, it will create a new one, which is what you are trying to achieve.

Source: Screen man page