I am running Linux 3.10.0-693.2.2.el7.x86_64, and for life it is impossible for me to understand why this will happen. It seems to be a bug and the shellchecker does not detect any problems.
#!/bin/bash
set -o nounset
options=(one two three)
select var in "${options[@]}"; do
if (( REPLY <= ${#options[@]} )) && [[ $REPLY =~ ^[0-9]+$ ]]; then
case $var in
one) exit;;
two) df -h /tmp;;
*) echo $var;;
esac
break
else
printf "Invalid selection.\n" >&2
fi
I used set -xv to troubleshoot, but here's the exit without it. During production, the options [@] will be set by the command, and the number they return will be dynamic. So I want the menu to execute the command in *) in $ var--, but I have to check the selection outside of limits in REPLY. Here is the result.
$ bad_select.bash
1) one
2) two
3) three
Invalid selection.
/home/lc5550358/bin/select_menu.wip: line 9: t: unbound variable
t
which I typed? I can also avoid an unbound variable, but type in k=2
or var
(the latter is defined in select). Why and what is happening around (required set -o nounset
)?