Visit Our UNIX and Linux User Community

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

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Execute function as soon as input is provided in menu drive shell script
# 1  
Old 06-20-2016
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:

            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:



# Menu Item


while :
	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 ;;

  echo "option 1"
  echo "\nPress ENTER To Continue..."

	echo "option 2"
	echo "\nPress ENTER To Continue..."

	echo "option 3"
	echo "\nPress ENTER To Continue..."

	echo "option 4"
	echo "\nPress ENTER To Continue..."



Last edited by kiran_j; 06-20-2016 at 04:37 AM..
# 2  
Old 06-20-2016
Does your bash's read offer the -n option:
-n nchars
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.
# 3  
Old 06-20-2016
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?

# 4  
Old 06-20-2016
read -n1 m_menu

This User Gave Thanks to stomp For This Post:
# 5  
Old 06-20-2016
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.

# 6  
Old 06-20-2016

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:
           ( Item 54 for shell, Verified 2016.06.20 )

        4) With dd and stty, encapsulated in a function:
                  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
           ( Verified 2016.06.20, compiles with warning, worked in 64-bit )

Best wishes ... cheers, drl

Edit 1: added link to old qprompt code.

Last edited by drl; 06-20-2016 at 11:45 AM..

Previous Thread | Next Thread
Test Your Knowledge in Computers #247
Difficulty: Easy
By 1973, the File Transfer Protocol (FTP) specification had been defined (RFC 354) and implemented, enabling file transfers over the ARPANET.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to iterate Grep via all patterns provided in an input file?

When I use the following grep command with options -F and -f, its just displaying the text related to only the last pattern. Command: $ grep -f pattern_file.txt input_file.txt Output: doc-C2-16354 Even the following command yields the same output: Command: $ grep -Ff pattern_file.txt... (6 Replies)
Discussion started by: nsai
6 Replies

2. Shell Programming and Scripting

Need help to write a function in shell scripting to execute sql files

Hi, I am new to shell scripting and i need to write a automation script to execute sql files. I need to check the table if it is there in system tables and need to write a function to call the .sql files. For ex. I have a.sql,b.sql,c.sql files, where the sql file contains DELETE and INSERT... (1 Reply)
Discussion started by: Samah
1 Replies

3. Shell Programming and Scripting

Script to select the rows from the feed file based on the input value provided

Hi Folks, I have the below feed file named abc1.txt in which you can see there is a title and below is the respective values in the rows and it is completely pipe delimited file ,. ... (3 Replies)
Discussion started by: punpun66
3 Replies

4. Shell Programming and Scripting

Want to use function and execute the below query in script.

#!/bin/bash function getDeliveredDispatches(firstDateTime, lastDateTime, limit) { var cursor = db.dispatches.find( {$and: }} ]}, {"deliveryGroupId" : 1, "batchId": 1, "statusHistory.code" : 1} ); var wrongDispatchesIds = ; print("Number of dispatches selected based on filter = " +... (2 Replies)
Discussion started by: neel2462
2 Replies

5. UNIX for Dummies Questions & Answers

[ASK]execute shell with function in solaris

dear all i need your advice in shell with solaris i have testing script like this #!/usr/bin/bash function test(){ echo "testing only" } ## execute function ## test but if i running always got error like this syntax error at line 1: `(' unexpected who can i running this... (7 Replies)
Discussion started by: zvtral
7 Replies

6. Shell Programming and Scripting

Shell Script to execute Oracle query taking input from a file to form query

Hi, I need to query Oracle database for 100 users. I have these 100 users in a file. I need a shell script which would read this User file (one user at a time) & query database. For instance: USER CITY --------- ---------- A CITY_A B CITY_B C ... (2 Replies)
Discussion started by: DevendraG
2 Replies

7. Shell Programming and Scripting

function from shell input

Hi all i have the command called printer which needs a usernmae for example i would run the commnad printer <username> how can i write i quick script in perl that if the username is ab then write "denined" and if it is anything else it will work thanks Adam (1 Reply)
Discussion started by: ab52
1 Replies

8. Shell Programming and Scripting

Help to write a script or program to automatic execute and link input file data

Output file template format <input_file_name>a</input_file_name> <total_length_size>b</total_length_size> <log_10_length_size>c</log_10_length_size> Input_file_1 (eg. sample.txt) SDFSDGDGSFGRTREREYWW Parameter: a is equal to the input file name b is equal to the total length of... (2 Replies)
Discussion started by: perl_beginner
2 Replies

9. Shell Programming and Scripting

How to use ssh execute other shell script on other host (shell script include nohup)?

i want use ssh on the host01 to execute on the host02 like following : host01> ssh host02 include nohup command like follwing : nohup /home/jack/ & after i execute ssh host02 one the host01. i can't found (1 Reply)
Discussion started by: orablue
1 Replies

10. Shell Programming and Scripting

Execute interactive bash menu script in browser with PHP

I have an interactive menu script written in bash and I would like use PHP to open the interactive bash menu in a browser. Is this possible? Using the sytem() function in php runs the script but it's all garbled. Seems like maybe a terminal window needs to be opened in php first? ... (1 Reply)
Discussion started by: nck
1 Replies

Featured Tech Videos