1

Here is a portion of my script:

read main_menu

case "$main_menu" in

    "0" )   exit
            ;;
    "1" )   cp /etc/bamt/cgminer.conf.X11 /etc/bamt/cgminer.conf;
            sudo mine restart;
            ;;

How can I make it so after the user enters 0 or 1, 2 etc, he doesn't have to press the Enter key? Practically, when you enter the number it would directly jump to the next menu or function without the needing to press Enter.

Can you help me implement it here?

while :
do
    echo -e "\n Test script"
    echo -e "\t (0) Exit"
    echo -e "\t (1) Option 1"
    echo -n "Enter choice:"
      read main_menu
      case "$main_menu" in
         "0" ) exit
         ;;
         "1" ) exit
         ;;
esac
done
0

1 Answer 1

7

Bash read supports a number of options, among them -n 1:

$ read -n 1 main_menu
1$ echo $main_menu
1

The -n option means that:

read returns after reading nchars characters rather than waiting for a complete line of input, but honor a delimiter if fewer than nchars characters are read before the delimiter.

-n 1 tells read to return after reading a single character of input, so as soon as the user presses 1 then read will stop and save the input into the variable.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.