![]() |
What determines the output order from a select statement
What determines the order a - select opt in "${!ListOfNames[@]}" Quit - statement outputs?
The script ReadTXTFile.sh Code:
#!/bin/bash Code:
charlie@charlie-machine:~/bin/DATA$ ls |
It's undefined.
You're reading the list into a hash table (well, an "associative array", which is almost certainly implemented using a hash table), and then just printing out the hash table as-is. Hash tables have undefined ordering. |
I agree that you should not use an associative array. There are several ways to create an array from a file.
bash 4 readarray -t ListOfNames < "$filename" read -r -a ListOfNames <<< "$(filename)" declare -A ListOfNames read -r line; do ListOfNames+=("$line") done < "$filename" Change your select line to select opt in "${ListOfNames[@]}" Quit |
Quote:
Code:
charlie@charlie-machine:~$ ReadTXTFile.sh Code:
#!/bin/bash |
Quote:
Cheers |
I guess I wasn't precise enough in my post. Switch to loading a standard array as suggested in my post and also change the select line.
|
+1 to michaelk. Replace the while loop with one mapfile command.
|
I don't understand why do we need associative array at all. As a minimal modification (I would do something like):
Code:
#!/bin/bash |
Quote:
Code:
charlie@charlie-machine:~$ mapfile -t < bin/DATA/TVNames.txt |
Quote:
|
Ok, great.
If you think your question is answered please mark the thread solved. Also if you wish to say thanks just click on yes. |
All times are GMT -5. The time now is 03:58 AM. |