屏幕中的 Bash 菜单 -ls data

屏幕中的 Bash 菜单 -ls 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.

我想出了一个为独立控制台生成菜单的脚本。

#!/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[0]}"
                        IFS="_" read -r -a name <<< "${data[1]}"
                        names[IDX]="${name[0]}"
                        ((++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

问题

这个脚本可以变得更加优雅和人类可读吗?

编辑 2019 年 10 月 20 日:

更新了脚本以在其中一个屏幕被破坏或添加其他屏幕的情况下重建其数组。建议:欢迎。

相关内容