LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-04-2006, 05:00 PM   #1
jimwelc
Member
 
Registered: Nov 2004
Location: Boston
Posts: 30

Rep: Reputation: 15
How can you determine if a user pressed the enter key?


Hello All,

I'm new to the scripting world, and I am trying to do something that should I thought would be trivial but I seem to be running into some difficulty. Basically I have a Bash script which is prompting a user for a key. Sounds like a simple task, but if a user presses the enter key I would like to use a default value. Unfortunately I can not seem to distinguish between an enter key, a space, backspace, delete. Below is my code snippet, any help you can provide would be greatly appreciated.

------ Begin Code Snippet --------
echo "Would you like to do something? [Y]:
read -n 1 answer

if [ "$answer" == "y" ] || [ "$answer" == "Y" ]
then
echo "User would like to do something"

else
echo "User did NOT want to do something"
exit 1
fi

------ End Code Snippet --------

It seems that when ever a enter key(carriage return, space, backspace, delete...etc) my return value stored is a NULL character.

So basically I am just looking is someone knows of a way way to determining if a user pressed the Enter Key?
 
Old 04-04-2006, 07:38 PM   #2
cramer
Member
 
Registered: Feb 2006
Distribution: Red Hat 9
Posts: 112

Rep: Reputation: 15
Try making it just "" instead of "y" if you want to just make it like a script where the user can change the value if needed but if not, just press enter to continue. Maybe that would work?
 
Old 04-04-2006, 07:48 PM   #3
cramer
Member
 
Registered: Feb 2006
Distribution: Red Hat 9
Posts: 112

Rep: Reputation: 15
Or maybe this helps a little, this is from the setup.sh script in webmin. Just as an example

---------------------------------------------------------
printf "Login name (default admin): "
if [ "$login" = "" ]; then
read login
if [ "$login" = "" ]; then
login="admin"
fi
fi
---------------------------------------------------------
printf asks the user what they want the value to be. then it checks to see what was entered, reads if as login and then checks to see if the input was empty (pressed enter), if it was then it assigns the default value.

Last edited by cramer; 04-04-2006 at 07:57 PM.
 
Old 04-04-2006, 10:08 PM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Here are two variants (my preference is the first one):
Option 1: Uses shell function:
Code:
RESP=""
get_yesno ()
{
  while [ "$RESP" != "y" ] && [ "$RESP" != "n" ]; do
    echo "Enter 'y' or 'n'..."
    read -n 1 RESP
  done
}

echo "Do you want to quit?"
get_yesno
case $RESP in
  y) echo "You entered YES";;
  n) echo "You entered NO";;
  *) echo "You entered something weird..."
esac
# Option 2: Uses simple loop
Code:
RESP=""
while [ "$RESP" != "y" ] && [ "$RESP" != "n" ]; do
  echo "Do you want to quit?"
  read -n 1 RESP
  case $RESP in
    y) echo "You entered YES"; break;;
    n) echo "You entered NO"; break;;
    *) echo "You entered something weird..."
  esac
done
echo "Hasta la bye bye!"
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
atkbd.c : unknow key pressed wneimeijer SUSE / openSUSE 0 03-16-2006 10:58 AM
RHEL 3 - Keyboard send several chars when key pressed once lmx Red Hat 1 01-29-2005 11:46 PM
is certain key pressed at the moment ?? freeborn Programming 4 11-03-2004 03:13 AM
Unknown key pressed Khang Linux - Newbie 3 10-27-2004 04:12 PM
I am getting a ^H when the backspace key is pressed. The only time that it happens is mlawler Linux - Networking 2 04-29-2004 02:37 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration