First of all,
$(..) is highly recommended over `..`.
Second,
awk can take a filename as an input argument, so you don't need to use cat and a pipe. And if there's only one word per line, then you don't need $1.
Code:
awk '{print}' list.txt
Third, using
awk here is truly overkill anyway.
cat alone does exactly the same thing as the above command. Even better, just use a simple
< shell redirection. You can even put it directly in the select command and skip the variable.
Code:
p=$( cat list.txt )
#or
p=$( <list.txt )
#or
select CHOICE in $( <list.txt ); do
Fourth, an invalid choice in
select results in an empty variable. So simply add a test of some kind after the read to check whether it contains anything. If empty, then just let it loop again (or do whatever you want it to do), else issue a
break command to exit the loop (or again, whatever).
case statements are used most often here, but any conditional construct will do.
Code:
#!/bin/bash
select CHOICE in $( <list.txt ) ; do
case $CHOICE in
"") echo "Not a valid choice."
echo "Try again." ;;
*) echo "$CHOICE"
break ;;
esac
done
exit 0