LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 12-02-2005, 04:56 PM   #1
linmix
Senior Member
 
Registered: Jun 2004
Location: Spain
Distribution: FC5
Posts: 1,993
Blog Entries: 1

Rep: Reputation: 46
Bash: how to read <enter> key


I have the following code snippet:
Code:
#!/bin/bash
question ()
{
echo '(y for yes, n for no, any other key to cancel)'
read tmp
if [ $tmp = "y" ]; then
 echo "you typed 'yes'"
elif [ $tmp = "n" ]; then
 clear
elif [ $tmp = "c" ]; then
 kill 0
else question
fi
}
question
If I run this script and instead of typing any 'normal' key press <enter> the I get an error message:
Code:
./test.sh: line 6: [: =: unary operator expected
./test.sh: line 8: [: =: unary operator expected
./test.sh: line 10: [: =: unary operator expected
How can I get the script to react to <enter> the same way as to any key (except of course 'y', 'n' and 'c')?
 
Old 12-02-2005, 05:11 PM   #2
Andrew Benton
Senior Member
 
Registered: Aug 2003
Location: Birkenhead/Britain
Distribution: Linux From Scratch
Posts: 2,073

Rep: Reputation: 64
Try this
Code:
#!/bin/bash
question ()
{
echo '(y for yes, n for no, any other key to cancel)'
read tmp
case "$tmp" in
	y)
		echo "you typed 'yes'"
		;;
	n)
		clear
		;;
	c)
		kill 0
		;;
	*)
		question
esac
}
question
 
Old 12-02-2005, 05:14 PM   #3
Berhanie
Senior Member
 
Registered: Dec 2003
Location: phnom penh
Distribution: Fedora
Posts: 1,625

Rep: Reputation: 165Reputation: 165
the point is to use quotes (so, "$tmp" instead of $tmp).
 
Old 12-02-2005, 05:38 PM   #4
linmix
Senior Member
 
Registered: Jun 2004
Location: Spain
Distribution: FC5
Posts: 1,993
Blog Entries: 1

Original Poster
Rep: Reputation: 46
Thanks!!
it's the double quoting that does it (both in my snippet and in the 'case sollution suggested by Andrew.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Have to enter WEP-key over and again artificialGekko Linux - Wireless Networking 2 11-07-2005 08:11 AM
enter wep key....where?? bariswheel Mandriva 3 12-23-2004 04:19 AM
How do you enter a Wep key? yodaf Linux - Wireless Networking 3 08-27-2004 10:59 PM
How to enter my WEP key? stuart Linux - Wireless Networking 15 07-21-2004 10:32 AM
checking for the 'enter' key Error1312 Programming 1 02-25-2004 11:30 AM


All times are GMT -5. The time now is 01:37 PM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration