#!/bin/sh # # (c) 2009 Michael Wojciechowski # # Show a list of screen sessions belonging to the current user. # # For each session show PID of the screen process and the name of # innermost child process. # # When a session is selected and the user presses , "screen -r # -d" is executed to attach to this session. # DIALOG="/usr/bin/dialog" MENU_DIM_WIDTH=80 MENU_DIM_HEIGHT=40 TEMP="/tmp/sscr-$USER" SP=`ps -C screen -o pid=` LASTPID=-1 FollowPid () { newpid=`ps --ppid $1 -o pid=` if [ -n "$newpid" ] then FollowPid $newpid else LASTPID=$1 fi } if [ ! -x "${DIALOG}" ] then echo "This script needs ${DIALOG} to function. Abort." exit -1 fi ENTRIES="" COUNT=0 ENTRIES="$ENTRIES -1 Create" COUNT=`expr $COUNT + 1` for PID in `echo $SP` do FollowPid $PID NAME=`ps -p $LASTPID -o comm=` if [ "$NAME" != "screen" ] then ENTRIES="$ENTRIES $PID $NAME" COUNT=`expr $COUNT + 1` fi done if [ "$COUNT" -eq 0 ] then echo "No screen sessions to show." exit 0 fi ${DIALOG} --title "Which screen session?" --menu "Select screen session" $MENU_DIM_WIDTH $MENU_DIM_HEIGHT $COUNT $ENTRIES 2> $TEMP STATUS=$? case $STATUS in "0") ;; # pressed ok. *) echo "Script aborted by user ($STATUS)." rm -f $TEMP exit esac SESSION=`cat $TEMP|sed -e "s/\"//g"` rm -f $TEMP if [ "$SESSION" -eq "-1" ] then screen -d -m exec $0 fi exec screen -r -d $SESSION