0

I have sorted (descending) output in a file in table format from a query I did using aws CLI. I need to take a field from the output file and present all unique lines as an option for user to choose from. Here is an example of output file:

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

The goal is to present all volumes after reading above output and prompt user to enter volume number OR user can enter A for ALL so that system can restore the snapshot(s) for that volume. Something like this:

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

Here is what I have, how can I get the user to enter a number corresponding to the volume number or enter ALL to restore all? Should I be using an array for from the output file? How can I do this efficently?

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"
}

1 Answer 1

1

Look at the bash select command.

I put the following into a bash script:

#!/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
Sign up to request clarification or add additional context in comments.

2 Comments

Hi - thank you Richard. How can I read output from file (GetfileSnapId) into select command and a array? I would like to use output from any aws region and present it for user to select any of the volumes or select all volumes.
I see your problem. Working it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.