Quitting the menu on RETURN with the PS3 prompt in bash

361 views Asked by At

I'm making a menu with for my terminal so that when I open it it will present me with a set of locations for me to go to.

It works fine but I'd like some extra things added.

This is my code

PS3="
Choose: "
select option in Tweaks Desktop "/" "~"
do
    case $option in
        Tweaks) cd "/Users/jontelang/Dropbox/Tweaks/"; break;;
        Desktop) cd "/Users/jontelang/Desktop"; break;;
        \/) cd "/"; break;;
        \~) break;;
     esac
done

And it give me this output:

1) Tweaks
2) Desktop
3) /
4) ~

Choose: 

When I press RETURN it just shows me the menu again, I'd like to make the menu simply disappear when RETURN is pressed. Anyone know how to do this?

I tried adding *), #?) and variations of those with a break;; inside but no dice. It works if I enter SPACE then RETURN but this is what I'd like to avoid (2 keystrokes versus 1).

I'd also like to know if there is some way to just let me press the keys (1-4) without having to press RETURN after.

1

There are 1 answers

0
jontelang On

Alright I skipped the whole PS3 thing and just went with read and some simple echoes. I'll put this as the answer but if anyone comes up with a definitive answer to the actual problem I'll set that as the answer.

echo "1) Desktop";
echo "2) Tweaks";
echo "3) /";
echo "4) ~";

read -p "Choose: " -n 1 -r
case $REPLY in
 1) cd ~/Desktop; echo " ";;
 2) cd ~/Dropbox/Tweaks; echo " ";;
 3) cd /; echo " ";;
 *) cd ~ ;;
esac

The echoes after each cd is just for a newline, personal preference.