Visit The New, Modern Unix Linux Community


BASH: Any Way to Get User Input Without Requiring Them to Hit the Enter Key?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting BASH: Any Way to Get User Input Without Requiring Them to Hit the Enter Key?
# 1  
BASH: Any Way to Get User Input Without Requiring Them to Hit the Enter Key?

I'm working on making a menu system on an HP-UX box with Bash on it. The old menu system presents the users with a standard text menu with numbers to make selections. I'm re-working the system and I would like to provide something more akin to iterative search in Emacs.

I have a list of 28 objects in an array that I want to match against as they type. So lets say the first four elements are:

ALU
BOR
CAD
CHR


If the user started with an "a" they would automatically get ALU since there are no other A elements. If they typed "c" however, they would get CAD with CHR printed just beneath it until they either type "a", "h" or something else. If they type "a" as the next letter, they get CAD and CHR disappears from the list. It they type "h" as the next letter, they get CHR and CAD disappears from the list. If they type "x" for example, then they get a brief "No Match" message and return to "C".

My thought is that to pull this off I need something other than 'read' since that requires you to press the enter key. I need something that will capture the keystroke and append it to a variable like this:

Pass 1: (user enters C)

MATCH="C"
# code here to check the entry against the list in the array
echo $MATCH # This prints out "C"

Pass 2: (user enters A)

MATCH=$MATCH$ENTRY
# code here to check the entry against the list in the array
echo $MATCH # This prints out "CA"

Pass 3: (user presses backspace)

MATCH=${MATCH:0:$((${#MATCH}-1))}
# code here to check the entry against the list in the array
echo $MATCH # This prints out "C" again

Pass 4: (user enters X)

MATCH=$MATCH$ENTRY
# code here to check the entry against the list in the array
# no match so...
echo "No Match" ; sleep 2s ; clear
MATCH=${MATCH:0:$((${#MATCH}-1))}
echo $MATCH # This prints out "C"

Is this even a possibility in Bash or should I be looking elsewhere?
# 2  
In BASH you can specify how many characters to read like 'read -n 1 C'. Read will return after 1 character input and place its value in C. Also, -s will also cause the input to not be echoed to the screen.
# 3  
Exactly what I needed! Thank you very much. Now we'll see if I can actually make my plan work...

Quote:
Originally Posted by Corona688
In BASH you can specify how many characters to read like 'read -n 1 C'. Read will return after 1 character input and place its value in C. Also, -s will also cause the input to not be echoed to the screen.

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #791
Difficulty: Easy
Logic gates cannot be used to store data.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Expect command to send the user input enter or ctrl+c

Hey All, I am writing one script using expect, that script which is used in spawn will accepts only 1. Enter 2. Ctrl+c Press Control-C to exit, Enter to proceed. Could some one share some thoughts to send the above user inputs in linux expect block ? Thanks, Sam (0 Replies)
Discussion started by: SCHITIMA
0 Replies

2. Shell Programming and Scripting

Input password to bash script, save, and enter when needed

I am looking for a way to start a script and have it prompt for a password that will be used later on in the script to SSH to another host and to SFTP. I don't want the password to be hard coded. Below is my script with the actual IP's and usernames removed. #!/usr/bin/expect -f... (2 Replies)
Discussion started by: jbrass
2 Replies

3. Shell Programming and Scripting

Reading user input...problem with tab key

Hi all, I have a little problem with my shell script (reading user input, save user input to variable, invisible characters in the log file :() printf "1. What's your file path?" /path/to/my/file read -e FILE I have invisible characters in my log file (e.g. <ESC> or ^G) when I'm... (3 Replies)
Discussion started by: splendid
3 Replies

4. Shell Programming and Scripting

How to pass enter key or selected character in bash script?

hi, i've bash script thats working... but now i need to add a line....that prompts for user input....like yes and 1 as complete install.... so here's how it looks... $ cd 9200 (cd into directory) $./install (hv to type ./install to run install then ask for) ----do you want to... (4 Replies)
Discussion started by: kernel11
4 Replies

5. Shell Programming and Scripting

Loop logic, enter into respective IF as per enter input file name

have three big data file, however I just need to see the mentioned below one line form the all the file which has SERVER_CONNECTION Value File 1 export SERVER_CONNECTION=//dvlna002:10001/SmartServer File2 export SERVER_CONNECTION=///SmartServer File3 export... (1 Reply)
Discussion started by: Nsharma3006
1 Replies

6. Shell Programming and Scripting

Creating a script requiring a pause for user input

Hi I'm trying to create a basic script that pauses for user input to verify a file name before generating the output. I have numerous SSL certificate files which I am trying to determine the expiry date so what I'm trying to do is write a script so that is pauses to request the name of the .pem... (9 Replies)
Discussion started by: Buddyluv
9 Replies

7. Shell Programming and Scripting

enter key or carriage return as input in perl

hi experts Question in perl i'm creating a script to take from user a different inputs one of them is the carriage return .. so that i want to make an if condition if the user hit enter key the user will go to previous step it something like that chomp ($input = <STDIN>); if ($input =~... (3 Replies)
Discussion started by: doubando
3 Replies

8. Shell Programming and Scripting

How to enter a return key in bash script?

Hi, I'm porting an install script from AIX to Red Hat (2.6.18-164.el5 #1 SMP) I have this script working in both AIX and HP-UX. The script is a wrapper for a Micro Focus Server Express install program. It responds to the install program questions with a here-now list. Responses includes... (14 Replies)
Discussion started by: duker61
14 Replies

9. UNIX for Dummies Questions & Answers

Pressing backspace key simulates enter key

Hi, Whenever i press the backspace key, a new line appears, i.e. it works like a enter key. :confused: Thanks (2 Replies)
Discussion started by: vibhor_agarwali
2 Replies

10. UNIX for Dummies Questions & Answers

hit 'enter' with no user intervention

I'd like to write a shell script that will enter '1', for example. How do you do that? You know, so instead of writing echo "Enter 1 or 2" read onetwo and waiting for the user to enter a 1 or 2, I'd like the script to act like someone entered a 1, or 2. Is there a hex value for the... (1 Reply)
Discussion started by: jpprial
1 Replies

Featured Tech Videos