Automatic screens

Automatic usage of screen to pass through network outages:

# Add the following line to your .bashrc
# if [[ -f ~/.screens.sh ]]; then source ~/.screens.sh && screens; fi

function screensNew
{
        read -p "Enter screen name or [RETURN]: " SCREEN_NAME
        if [[ ! -z "$SCREEN_NAME" ]];
        then
                echo "Starting new $SCREEN_NAME screen !";
                screen -S "$SCREEN_NAME"
        else
                echo "Starting new unamed screen !";
                screen
        fi
}

function screensMenu
{
        i=1;
        for SCREEN_INFO in $SCREEN_INFOS;
        do
                SCREENS[ $i ]="$SCREEN_INFO";
                SCREEN_IDS[ $i ]="$( echo $SCREEN_INFO | awk '{print $1}')";
                echo "$i: ${SCREENS[ $i ]}";
                i=$(( $i + 1 ));
        done;
        read -p "Please enter screen ID to attach or Q(uit) or [RETURN] for new screen: " SCREEN_ID;
        if [[ -z "$SCREEN_ID" ]];
        then
                screensNew
        elif [[ "$SCREEN_ID" == [qQ] ]];
        then
                return
        else
                if [[ ! -z "${SCREEN_IDS[ $SCREEN_ID ]}" ]];
                then
                        echo "Attaching to screen ${SCREEN_IDS[ $SCREEN_ID ]}";
                        screen -x -r ${SCREEN_IDS[ $SCREEN_ID ]}
                else
                        echo "Screen #$SCREEN_ID not found !";
                fi;
        fi
}

function screens
{
        if [[ ! -z "$( ps x | grep SCREEN | grep -v grep | awk '{print $1}' | grep $PPID )" ]];
        then
                return
        fi
        if [[ -z "$( screen -ls | grep 'No Sockets found' )" ]];
        then
                echo 'Screens are available:';
                SCREEN_INFOS=$( screen -ls | grep tached );
                IFS=$'\n';
                screensMenu
        else
                echo 'No Screen available...';
                read -p "Please enter Q(uit) or anything else to start a new screen: " SCREEN_ID;
                if [[ "$SCREEN_ID" != [qQ] ]];
                then
                        screensNew
                fi
        fi;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s