Skip to content

Instantly share code, notes, and snippets.

@brimston3
Last active December 22, 2015 14:49
Show Gist options
  • Select an option

  • Save brimston3/6488291 to your computer and use it in GitHub Desktop.

Select an option

Save brimston3/6488291 to your computer and use it in GitHub Desktop.
.bashrc script to auto-attach screen if there is a session available on login.
#/bin/bash
#set -x
: <<'END'
autoscreen.bashrc
Andrew Domaszek
project: random bits of bash
Copyright (C) September 8, 2013, Andrew Domaszek
(MIT License)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
## usage:
copy to $HOME/.bash/autoscreen.bashrc
Append to $HOME/.bashrc:
if [ -r "$HOME/.bash/autoscreen.bashrc" ]; then
. $HOME/.bash/autoscreen.bashrc
fi
END
# if interactive and not already inside a screen
if [ ! -z "$PS1" ] && [ -z "$STY" ]
then
old=$IFS
IFS=$'\n'
screens=($(screen -ls | egrep -iv '\(.*dead.*\)' | grep -e '(.*)' | awk '{print $1}'))
IFS=$old
if [ ${#screens[@]} -eq 1 ]; then
# exec this shell into the only screen session
exec screen -x "${screens[0]}"
elif [ ${#screens[@]} -gt 1 ]; then
# bring up a selection menu with the available screen sessions
select opt in "${screens[@]}" "None"
do
if [ "$opt" != "None" ] ; then
exec screen -x "$opt"
else
echo "Ok, not loading any screens"
break;
fi
done;
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment