Is there any way to implement a drop down menu selection in bash?
This is on CDLinux which is a very minimal live CD and I am using it to install an image onto a hard drive. Part of that process is the timezone selection. There are just too many timezones to attempt to use the "select" command. It would just scroll off the screen and be gone.
CDLinux has lynx and I concidered using it and an HTML form but there is no way to post the output to any type of cgi script without a webserver running on the machine.
If not in bash, then are there any handy programs I could compile that would implement a drop down menu selection?
OK, I wrote a script that does near-enough what I wanted to do...
#!/bin/bash
#Read time zones into array
i=1; for item in `cat timezones.txt`; do tz[$i]="$item"; let i=$i+1; done
i=1
echo "Locate timezone using up/down arrow keys then press enter to select."
while [ 0 ]; do
if [ $i -eq 0 ]; then i=1; fi
if [ ! ${tz[$i]} ]; then let i=i-1; fi
echo -en "\r "
echo -en "\r${tz[$i]}"
read -sn 1 twiddle
case "$twiddle" in
"B")
let i=i+1
;;
"A")
let i=i-1
;;
"")
echo
echo "OK to select ${tz[$i]} (y/N)?"
read -sn 1 confirm
if [ "$confirm" == "y" -o "$confirm" == "Y" ]; then
break
else
echo "Locate timezone using up/down arrow keys then press enter to select."
fi
;;
esac
done
echo
echo "Final choice was ${tz[$i]}"
The above script is fine for newer bash however CDLinux contains a really old version of bash that doesn't support the -en switches for the read command nor does it support arrays. After much faffing I managed to re-write the script to be backward compatible.
#!/bin/bash
#Read time zones into array
function key_mode() {
if [ $1 -a $1 = "on" ]; then
stty_state=`stty -g`
stty raw; stty -echo
else
stty "$stty_state"
fi
}
i=1; for item in `cat timezones.txt`; do eval tz$i="$item"; let i=$i+1; done
i=1
echo "Locate timezone using up/down arrow keys then press enter to select."
key_mode on
while [ 0 ]; do
eval selection=\$tz$i
if [ $i -eq 0 ]; then
i=1
eval selection=\$tz$i
fi
if [ ${#selection} -lt 2 ]; then
let i=i-1
eval selection=\$tz$i
fi
echo -en "\r "
echo -en "\r$selection"
twiddle=`dd bs=1 count=1 2>/dev/null`
#read -sn 1 twiddle
case "$twiddle" in
`echo -e "\102"`)
let i=i+1
;;
`echo -e "\101"`)
let i=i-1
;;
`echo -e "\015"`)
key_mode off
echo
echo "OK to select $selection (y/N)?"
key_mode on
confirm=`dd bs=1 count=1 2>/dev/null`
if [ "$confirm" = "y" -o "$confirm" = "Y" ]; then
break
else
key_mode off
echo "Locate timezone using up/down arrow keys then press enter to select."
key_mode on
fi
;;
esac
done
key_mode off
echo
echo "Final choice was $selection"