1

Given the screen data

There are screens on:
    27454.world-of-dragons_SERVER   (07/05/2018 04:38:56 PM)        (Attached)
    6223.potato-wod_SERVER  (07/05/2018 10:16:12 AM)        (Attached)
    1681.potato-wod_MASTER  (07/04/2018 10:06:20 PM)        (Detached)
    30448.world-of-dragons_MASTER   (07/04/2018 09:06:01 PM)        (Detached)
4 Sockets in /var/run/screen/S-kreezxil.

I have come up with a script that generates a menu for the Detached consoles.

#!/bin/bash
IDX=0
declare -a pids
declare -a names

for f in $(screen -ls); do
    if [[ $f = *"MASTER"* ]]; then
            IFS="." read -r -a data <<< "$f"
            pids[IDX]="${data[0]}"
            IFS="_" read -r -a name <<< "${data[1]}"
            names[IDX]="${name[0]}"
            ((++IDX))
    fi
done

size=${#pids[@]}

while true; do
    clear
    printf '\nSCREEN\tPID\tNAME\n'

    for (( i=0; i<${size}; i++ )); do
            printf '%s\t%s\t%s\n' ${i} ${pids[i]}  ${names[i]}
    done

    printf '\nWhich screen # would you like to resume?\nEnter to q to quit or exit\n'
    read n

    if [ $n == 'q' ]; then
            exit
    fi

    if [ $n -lt 0 || $n -gt $size ]; then
            read -p "Invalid Option: Press [Enter] to try again" readEnterkey
            continue
    fi

    screen -r ${pids[$n]}
done

Can select be used to make the script more elegant, if yes, how? (this is prime question)

Note: I want to maintain readability of the script. Suggestions: welcomed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.