What determines the output order from a select statement
by GPGAgent from LinuxQuestions.org on (#5230A)
What determines the order a - select opt in "${!ListOfNames[@]}" Quit - statement outputs?
The script ReadTXTFile.sh
Code:#!/bin/bash
declare -A ListOfNames
filename="$HOME/bin/DATA/TVNames.txt"
n=1
while read line; do
# reading each line
ListOfNames[$line]=$n
n=$((n+1))
done < $filename
COLUMNS=20
PS3="Please enter your choice: "
select opt in "${!ListOfNames[@]}" Quit
do
[[ "$REPLY" =~ [^0-9] ]] && (( REPLY = $# + 1 ))
if (( REPLY <= ${#ListOfNames[*]} + 1 ))
then
break
else
echo "ERROR: Invalid choice. Must choose one of the numbers provided."
fi
done
if [[ "$opt" == Quit ]]
then
echo "quitting"
else
echo "$opt"
#
# cat <<-OUT
# =====================================
# Name of TV person is: $opt
# Prompt for filename, title, artist etc
# =====================================
# Now this will continue with the processing
# part of the script.......
# ====== Finished Okay ================
# OUTThe above code reads this list from a file:
Code:charlie@charlie-machine:~/bin/DATA$ ls
TVNames.txt
charlie@charlie-machine:~/bin/DATA$ cat TVNames.txt
Carol Kirkwood - BBC Weather Girl
Nina Warhurst - BBC News Girl
Lorraine Kelley - ITV
Victoria Graham - BBC Spotlight
Kirstie Allsopp - Property Expert
Catherine Southon - Antiques Expert
Nicki Chapman - Wanted Down Under
Holly Willoughby - ITV
IMAGES-
charlie@charlie-machine:~/bin/DATA$ ReadTXTFile.sh
1) Holly Willoughby - ITV
2) Catherine Southon - Antiques Expert
3) IMAGES-
4) Victoria Graham - BBC Spotlight
5) Carol Kirkwood - BBC Weather Girl
6) Kirstie Allsopp - Property Expert
7) Nicki Chapman - Wanted Down Under
8) Nina Warhurst - BBC News Girl
9) Lorraine Kelley - ITV
10) Quit
Please enter your choice:But outputs in a different order.


The script ReadTXTFile.sh
Code:#!/bin/bash
declare -A ListOfNames
filename="$HOME/bin/DATA/TVNames.txt"
n=1
while read line; do
# reading each line
ListOfNames[$line]=$n
n=$((n+1))
done < $filename
COLUMNS=20
PS3="Please enter your choice: "
select opt in "${!ListOfNames[@]}" Quit
do
[[ "$REPLY" =~ [^0-9] ]] && (( REPLY = $# + 1 ))
if (( REPLY <= ${#ListOfNames[*]} + 1 ))
then
break
else
echo "ERROR: Invalid choice. Must choose one of the numbers provided."
fi
done
if [[ "$opt" == Quit ]]
then
echo "quitting"
else
echo "$opt"
#
# cat <<-OUT
# =====================================
# Name of TV person is: $opt
# Prompt for filename, title, artist etc
# =====================================
# Now this will continue with the processing
# part of the script.......
# ====== Finished Okay ================
# OUTThe above code reads this list from a file:
Code:charlie@charlie-machine:~/bin/DATA$ ls
TVNames.txt
charlie@charlie-machine:~/bin/DATA$ cat TVNames.txt
Carol Kirkwood - BBC Weather Girl
Nina Warhurst - BBC News Girl
Lorraine Kelley - ITV
Victoria Graham - BBC Spotlight
Kirstie Allsopp - Property Expert
Catherine Southon - Antiques Expert
Nicki Chapman - Wanted Down Under
Holly Willoughby - ITV
IMAGES-
charlie@charlie-machine:~/bin/DATA$ ReadTXTFile.sh
1) Holly Willoughby - ITV
2) Catherine Southon - Antiques Expert
3) IMAGES-
4) Victoria Graham - BBC Spotlight
5) Carol Kirkwood - BBC Weather Girl
6) Kirstie Allsopp - Property Expert
7) Nicki Chapman - Wanted Down Under
8) Nina Warhurst - BBC News Girl
9) Lorraine Kelley - ITV
10) Quit
Please enter your choice:But outputs in a different order.