Sample Header Ad - 728x90

Bash menu from screen -ls data

2 votes
0 answers
196 views
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

declare -a pids
declare -a names

build_arrays() {
        IDX=0
        pids=()
        names=()

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

declare -p

while true; do
        build_arrays

        size=${#pids[@]}

        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
## The Question Can this script be made more elegant and human readable? ## Edit 10/20/2019: Updated the script to rebuild it's arrays in the event that one of the screens are destroyed or other screens get added. Suggestions: welcomed.
Asked by Kreezxil (75 rep)
Jul 5, 2018, 07:25 PM
Last activity: Jan 25, 2020, 09:19 PM