【问题标题】:File output into an array using bash使用 bash 将文件输出到数组中
【发布时间】:2020-04-22 22:07:09
【问题描述】:

我已使用 aws CLI 执行的查询以表格格式对文件中的输出进行排序(降序)。我需要从输出文件中获取一个字段并将所有唯一行作为用户选择的选项。这是一个输出文件的例子:

Description        Snapshot             StartTime
Volume0            snap-x123456789      2020-04-22T20:55:10
Volume10           snap-y123456789      2020-04-22T20:45:09
Volume12           snap-a123456789      2020-04-22T20:40:08
Volume15           snap-b123456789      2020-04-22T20:35:07
Volume0            snap-c123456789      2020-04-22T20:30:06
Volume10           snap-d123456789      2020-04-22T20:25:05

目标是在阅读上述输出后显示所有卷,并提示用户输入卷号,或者用户可以为 ALL 输入 A,以便系统可以恢复该卷的快照。像这样的:

0 - Volume0
10 - Volume10
12 - Volume12
15 - Volume15

这就是我所拥有的,如何让用户输入与卷号对应的数字或输入ALL以恢复所有?我应该使用输出文件中的数组吗?我怎样才能有效地做到这一点?

output() {

    echo "Here is a list of volumes found in $region"

    for DR in (grep Volume# "$input" | top -n 1)
    do 
    echo $DR
    done

    echo "Hello, please tell me what Volume you are searching for..(Volume?):"
    read volSearch

    echo -e "Searching for newest SnapshotIds in region using output file GetfileSnapId for:\n" $volSearch
    echo
    sleep 5
    input="/Users/user/Downloads/GetfileSnapId"


    if x=$(grep -m 1 "$volSearch" "$input")
    then
        echo
        echo "$x"
    else
        echo
        echo "$volSearch not found..ending search"
    fi

    extractSnap=$(echo "$x" | cut -d'|' -f2 )

    echo
    echo $extractSnap

    regionoutput=$(echo "$extractSnap" | awk '{print $4}' )
    echo
    echo "$regionoutput"
}

【问题讨论】:

    标签: bash amazon-web-services command-line-interface


    【解决方案1】:

    查看 bash 选择命令。

    我将以下内容放入 bash 脚本中:

    #!/bin/bash
    
    options=($(tail +2 t.txt | cut -d' ' -f1 | sort | uniq) Quit)
    
    PS3='Please enter your choice: '
    select opt in "${options[@]}"
    do
        echo $opt
    
        if [ $opt = "Quit" ]
        then
            break
        fi
    done
    

    【讨论】:

    • 嗨 - 谢谢理查德。如何将文件(GetfileSnapId)的输出读取到选择命令和数组中?我想使用任何 aws 区域的输出并将其呈现给用户以选择任何卷或选择所有卷。
    • 我看到了你的问题。工作它。
    猜你喜欢
    • 1970-01-01
    • 2019-06-30
    • 1970-01-01
    • 2011-08-14
    • 2020-12-17
    • 1970-01-01
    • 2011-05-02
    • 2013-02-12
    • 1970-01-01
    相关资源
    最近更新 更多