Some helpful functions for making interactive bash scripts on Linux terminal respond to cursor keys for data entry.
(and now not dependent on tput from ncurses)
Allow a user to select a path
source bash_ui.sh
CHOICES=`find . -maxdepth 1 -mindepth 1 -type d -printf '%f\n'`
choose_one
echo $CHOSEN
Ask user to select from a set of options
source bash_ui.sh
CHOICES=`find . -maxdepth 1 -mindepth 1 -type d -printf '%f\n'`
choose_multiple
echo $CHOSEN
source bash_ui.sh
read -r -d '' CHOICES <<EOT
1. Option 1=1
2. Yes, option 2=2
3. Believe it or not, option 3=3
EOT
choose_one
if [ "$CHOSEN" == "3" ]; then
echo "Somehow it really was option 3 that was selected!"
fi
source bash_ui.sh
read -r -d '' CHOICES <<EOT
one=1
two=2
three=3
all=1\n2\n3
EOT
choose_one
if [ ${CHOSEN} == "3" ]; then
echo "'three' which corresponds to value 3 was selected!"
fi
(welcome to shui! DOWNLOAD)
A helper script that does not need to be sourced, works in bash < 4 (hello macOS) and with Linux and Mac terminals
#!/bin/bash
result=$(find /home -maxdepth 1 -mindepth 1 -type d -printf '%f\n' | ./shui 2>&1 1>/dev/tty)
echo "Chosen home dir: $result"
#!/bin/bash
result=$(find /home -maxdepth 1 -mindepth 1 -type d -printf '%f\n' | ./shui multiple 2>&1 1>/dev/tty)
echo "Chosen home directories:"
echo "$result"
#!/bin/bash
echo "Choose your favourite characters"
read -r -d '' CHOICES <<- EOS
Roger Ellison
Duiwel Dewet
Roger Rabbit
EOS
result=$(echo -e "$CHOICES" | ./shui 2>&1 1>/dev/tty)