Execute function as soon as input is provided in menu drive shell script

Hi All,

I have a menu driven scripts. As you know while running the script we have to input the option such as 1,2, and 3 to execute function accordingly. but after selecting the input we have to press Enter.

My requirement is to execute function as soon as we press the option.

Is there any way to do this? Please suggest.

Display Menu:

Code:

-------------------------------------
            Main Menu
-------------------------------------
[1] Option 1
[2] Option 2
[3] Option 3
[4] Option 4
[5] Exit
=====================================
Enter your menu choice [1-5] [Default :1]:

Current Shell Script Code:

Code:

#!/usr/bin/bash

# Menu Item

main_menu()
{

while :
do
	clear
	echo " "
	echo "-------------------------------------"
	echo "            Main Menu "
	echo "-------------------------------------"
	echo "[1] Option 1"
	echo "[2] Option 2"
	echo "[3] Option 3"
	echo "[4] Option 4"
	echo "[5] Exit"
	echo "====================================="
	echo "Enter your menu choice [1-5] [Default : 1]: \c "
	read m_menu
	
	case "$m_menu" in

		1) option_1;;
		2) option_2;;
		3) option_3;;
		4) option_4;;
		5) exit 0;;
                "") option_1;;
		*) echo "\nOpps!!! Please Select Correct Choice";
		   echo "Press ENTER To Continue..." ; read ;;
	esac
done
}

option_1()
{
  echo "option 1"
  echo "\nPress ENTER To Continue..."
    read
	return
}

option_2()
{
	echo "option 2"
	echo "\nPress ENTER To Continue..."
    read
	return
}

option_3()
{
	echo "option 3"
	echo "\nPress ENTER To Continue..."
    read
	return
}

option_4()
{
	echo "option 4"
	echo "\nPress ENTER To Continue..."
    read
	return
}

main_menu

Regards,
Kiran

Does your bash 's read offer the -n option:

Hi RudiC,

Does your bash 's read offer the -n option:

=>Yes, It is there.

Can you guide me on how to make use of it?

Regards,
Kiran

read -n1 m_menu
1 Like

Thanks! I got it.

Is there another way if we do not have -n in read option.

I have observed that in linux server -n option is there but on my solaris server with ksh "-n" option is not there.

Regards,
Kiran

Hi.

Some possibilities:

Read one and only one character

        1) bash & ksh: read -n1

        2) zsh: read -k 1

        3) 56 examples in various codes at:
           https://rosettacode.org/wiki/Keyboard_input/Obtain_a_Y_or_N_response
           ( Item 54 for shell, Verified 2016.06.20 )

        4) With dd and stty, encapsulated in a function:
                getchar()
                {
                  local oldstty char
                  oldstty=$(stty -g)
                  stty cbreak -echo
                  char=$( dd bs=1 count=1 2>/dev/null )
                  stty $oldstty
                  echo "$char"
                }
            ...
            char=$( getchar )

        5) qprompt, timer, specific characters, prompt-string, etc.; in c
           http://www.ibiblio.org/pub/historic-linux/ftp-archives/sunsite.unc.edu/Nov-06-1994/utils/shell/qprompt-2.3.tar.gz
           ( Verified 2016.06.20, compiles with warning, worked in 64-bit )

Best wishes ... cheers, drl

Edit 1: added link to old qprompt code.