Skip to content

Instantly share code, notes, and snippets.

@memory
Created March 2, 2012 22:18
Show Gist options
  • Save memory/1961818 to your computer and use it in GitHub Desktop.
Save memory/1961818 to your computer and use it in GitHub Desktop.
automatic screen creation/reconnection from .bash_profile
# a quick and dirty little hack: never get screwed by the fact that you forgot to
# run "screen" on a remote host ever again!
#
# put this into your .bash_profile at the very end, or in a separate file and source
# it (again as the last action) from .bash_profile.
if [ "$TERM" != "screen" ]; then
output=$(screen -ls | grep ached)
screens=($(echo "$output" | awk '{print $1}'|xargs))
statuses=($(echo "$output" | awk '{print $2}'|xargs))
numscr=${#screens[@]}
if [ ${numscr} -gt 0 ]; then
n=0
echo
echo There are existing screen sessions:
echo
while [ $n -lt $numscr ]; do
echo "[$n] ${screens[$n]} ${statuses[$n]}"
n=$(($n+1))
done
echo
read -p "Select an existing screen or hit enter to start a new one. " ANSWER
if [ -z "$ANSWER" ]; then
exec screen
else
exec screen -DDRR ${screens[$ANSWER]}
fi
else
exec screen
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment