[SOLVED] Bash Script to Create Menu Based on a File List (Map Files List to Numbers)
by Steve R. from LinuxQuestions.org on (#55GQH)
As part of learning bash scripting, I developed a bash script to undertake an incremental back-up. The next question, was how to obtain a list of the back-up files and to select one (tar) archive file for listing all the files stored in that (tar) archive.
I found the solution here: Create bash menu based on file list (map files to numbers). It ran "right-out-of-the-box" with only obvious minimal tweaking. Seemed well worth re-posting here. See the answer on Ask Ubuntu to get full context. Credit goes to: deltab.
Code:echo "The following `*.war` archives were found; select one:"
# set the prompt used by select, replacing "#?"
PS3="Use number to select a file or 'stop' to cancel: "
# allow the user to choose a file
select filename in *.war
do
# leave the loop if the user says 'stop'
if [[ "$REPLY" == stop ]]; then break; fi
# complain if no file was selected, and loop to ask again
if [[ "$filename" == "" ]]
then
echo "'$REPLY' is not a valid number"
continue
fi
# now we can use the selected file
echo "$filename installed"
# it'll ask for another unless we leave the loop
break
done


I found the solution here: Create bash menu based on file list (map files to numbers). It ran "right-out-of-the-box" with only obvious minimal tweaking. Seemed well worth re-posting here. See the answer on Ask Ubuntu to get full context. Credit goes to: deltab.
Code:echo "The following `*.war` archives were found; select one:"
# set the prompt used by select, replacing "#?"
PS3="Use number to select a file or 'stop' to cancel: "
# allow the user to choose a file
select filename in *.war
do
# leave the loop if the user says 'stop'
if [[ "$REPLY" == stop ]]; then break; fi
# complain if no file was selected, and loop to ask again
if [[ "$filename" == "" ]]
then
echo "'$REPLY' is not a valid number"
continue
fi
# now we can use the selected file
echo "$filename installed"
# it'll ask for another unless we leave the loop
break
done