1

I want to output an array with filenames and let the user select the file to process.

Currently I have the following:

patches=( $(ls $WORKING_DIR/PATCH_*) )
echo "Select available Patch to apply"
for i in "${!patches[@]}"; do
    echo  "$i"":" "${patches[$i]}"
done

echo "Line Number, followed by [ENTER]:"

read seleted_patch

echo "Patch to install:" "${patches[$selected_patch]}"

But _Patch to Install_ just outputs the first value of the patches array. How can I get the value of the array the user enters on the commandline?

2 Answers 2

1

Sorry to say, but it's just a typo.

Try changing the following line:

read seleted_patch

To:

read selected_patch
Sign up to request clarification or add additional context in comments.

7 Comments

Omg. Fail. Thanks! Why it outputs the 1st Value as default?
It's a good question - I'm not familiar enough with the intricacies of bash arrays - I generally feel that if you're using such advanced shell-scripting features, you're probably better off with a real programming language. I guess an undefined variable ($selected_patch in your code) somehow references the 0-place in the array, but I dunno why.
Due to the typo $selected_patch is empty and ${patches[]} has the same meaning as ${patches[0]}
I guess, except that ${patches[]} gives: ${patches[]}: bad substitution
Ups, sorry. Tested in ksh where both are valid. @develth You are really using bash?
|
1

You may use select (a command exactly designed for that purpose)

PS3= "Select available Patch to apply "
select patch in $WORKING_DIR/PATCH_*; do
    echo "Patch to install: $patch"
    break;
done

With added handling for out of range selection

select patch in $WORKING_DIR/*; do
    if [[ ! $patch ]]; then
        echo "Selection out of range"
        continue
    fi
    if [[ $REPLY = 'q' ]]; then
        echo "Quit selection"
        break
    fi
    echo "Patch to install: $patch"
    break;
done

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.