If I may add some suggestions:
# Show numerated file list
ls *.txt | cat -n | less
Yo do not need to pipe anything here, just do
less -N *.txt
# Check entry is numeric
if echo $answer | grep "^[0-9]*$" > /dev/null
Again, you do not need to pipe anything. This checks if it contains non numeric characters: a2 or 34s will fail the test
if [[ ! $answer = *[!0-9]* ]]
# check the number isn't bigger than the list of files
count=`ls *.txt | wc -l`
Don't ever parse the output of ls!!!. Insted:
f=(*.txt)
count=${#f[@]}
WARNING: this does not check if there are no .txt files <--- it will fail in that case as it will return a one file (the * glob)
ourfile=`ls *.txt | sed -n "$answer p"`
echo "You chose $ourfile"
I'm not quite sure what's your intention here... ;)
I'm assuming you are using bash, aren't you?
You should avoid `command` as it is deprecated and not easy to nest (try nesting 3-4 things and you'll understand what I mean). Try $(command) instead ;)
If you are using bash.... you should use [ [ instead of [ . The [ command (yes, it is a command, not a built in shell option) is, by far, less powerfull than the built in [ [
Regards