useful screen bash function
So I pretty much live in screen permanently these days, and there are a gazillion awesome introductions out there to screen that describe the amazing advantages of screen.
One thing that has been a minor niggle has been the clunky way you pick which screen session you wish to reattach to if you have multiple sessions active, so I whipped up this bash function that I’ve been finding really useful.
function scr() {
if [ ! $1 ]; then
screen -D -R
else
i=1
for j in $(screen -list | awk --posix '/^[[:space:]]*[[:digit:]]{2,}.*$/{print $1}'); do
screens[${i}]=$j
let i=i+1
done
# test if integer
if ! [ $1 -eq $1 2> /dev/null ]; then
echo "You must supply an integer as the argument"
elif [ $1 -eq 0 ]; then
echo "screens list count starts at 1, not zero."
elif [[ ${1} -gt ${#screens[@]} ]]; then
echo "only ${#screens[@]} screens exist"
screen -list
else
screen -D -R ${screens[$1]}
fi
fi
}
So you can basically go scr to reattach to a single screen session, scr 1 to reattach to the first, scr 2 to reattach to the second, etc etc.
I’m kind of expecting someone to pipe up now and point out a much easier way… :)
May 15th, 2008 at 10:50 pm
I certainly don’t claim any semblance of betterness, however I’ve never found the need to sort out which screen session to re-attach to.
My fundamental assumption is that there’s one screen session to rule them all. I call it TRUNK, and it’s bound to ^v rather than ^a.
So, I fire up the main screen session with screen -S TRUNK -e ^vV, and then sub-screen sessions are created inside, like screen -S DEV, screen -S SSH, etc…
So, even though lots of screens are running, I only need to do a screen -rx TRUNK to pick up right where I left off. I also find it nice to be able to attach multiple terminal windows to the same “trunk” screen session, but viewing different nested screens, e.g. SSH or DEV. This also effectively allows copying and pasting of text across different ssh endpoints.
Definitely a cool bash function though. Particularly good example of array use in shell scripts.
May 15th, 2008 at 11:11 pm
my normal rule with bash is if I’m using arrays, I should rewrite it in something else…
After you explained your setup yesterday, I spent a while trying to use it, but just couldn’t seem to get myself to work like that…
I never used to need to work out which session to reattach to, but since starting with a 30″ monitor attached to my laptop at work, I seem to lose the visual link between sessions when attaching/detaching the monitor and/or network.
Are you still just living in one single terminal instance and doing absolutely everything in screen? That might be the difference… I like having 5 specific terminal windows that have differing screen sessions.
August 9th, 2008 at 12:42 pm
Maybe I’m missing something, but what’s the rationale between using multiple instances of screen, given that you can attach to a single instance as many times as needed (from different terminals / hosts / whatever) using screen -x? Each simultaneous attached client can discretely switch between screen windows…
September 8th, 2008 at 10:16 pm
Yet another nifty function using screen (kind of plaything):
Screensaver as desktop background
http://codesnippets.joyent.com/posts/show/1508
September 9th, 2008 at 1:16 am
I’ve been trying to think why I like having multiple screen instances, and my only conclusion is that I’m used to it. I do use screen -x sometimes, but not consistently.
I do also have different profiles set up due to some internal tools I use requiring different keyboard layouts, backspace/delete, term emulators, etc etc, so I guess that’s part of it. I could just try and work out a unified profile…