LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-21-2010, 04:02 PM   #1
Slightly Disoriented
Member
 
Registered: Sep 2010
Distribution: Ubuntu 10.10
Posts: 87

Rep: Reputation: 2
How to make a bash script ask for a specific key.


I am attempting to learn how write bash scripts. I want one to ask for a specific key. For example, I want it to ask a question, then if it is like, y for yes, or n for no, it does a specific thing. I'm thinking it would be something like:
Code:
#!/bin/bash
echo "Are you a boy? y/n"
read -t3
then something... Please help!

Last edited by Slightly Disoriented; 11-21-2010 at 04:51 PM.
 
Old 11-21-2010, 05:11 PM   #2
Slightly Disoriented
Member
 
Registered: Sep 2010
Distribution: Ubuntu 10.10
Posts: 87

Original Poster
Rep: Reputation: 2
Oh, I wasn't really clear. I want it to, if possible, do a certain command if a certain key is pressed. Like, in the above script, if they pressed n then it would
Code:
echo "So you are a girl.
and if y then it would do something like
Code:
echo "Thats what I thought.
I know
Code:
if
would be used. I can not find it on google.
 
Old 11-21-2010, 05:36 PM   #3
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,819
Blog Entries: 1

Rep: Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200
This should get you started.
http://tldp.org/LDP/Bash-Beginners-G...ect_07_01.html

and this:
http://www.tech-recipes.com/rx/209/b...ng-comparison/

Last edited by sycamorex; 11-21-2010 at 05:38 PM.
 
Old 11-21-2010, 06:04 PM   #4
Slightly Disoriented
Member
 
Registered: Sep 2010
Distribution: Ubuntu 10.10
Posts: 87

Original Poster
Rep: Reputation: 2
Hmmm, well the way those made it look like it would be
Code:
#!/bin/bash
echo "Yes or No (Y/N)"
read $var
if [$var = "y" ]
then 
	echo "Yes"
else
	echo "No"
fi
echo "Done"
exit
But when I run that script, here's what comes back:
Code:
~$ cd Linux/Scripts && bash Response.sh && cd
Yes or No (Y/N)
y
Response.sh: line 4: [: =: unary operator expected
No
Done
I don't exactly get those sites... Keep in mind, I'm just starting, and I don't really have any other programming experience. Sorry, but do you what I need to do?
 
Old 11-21-2010, 06:07 PM   #5
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Quote:
Originally Posted by Slightly Disoriented View Post
Hmmm, well the way those made it look like it would be
Code:
#!/bin/bash
echo "Yes or No (Y/N)"
read $var
if [$var = "y" ]
then 
	echo "Yes"
else
	echo "No"
fi
echo "Done"
exit
But when I run that script, here's what comes back:
Code:
~$ cd Linux/Scripts && bash Response.sh && cd
Yes or No (Y/N)
y
Response.sh: line 4: [: =: unary operator expected
No
Done
I don't exactly get those sites... Keep in mind, I'm just starting, and I don't really have any other programming experience. Sorry, but do you what I need to do?
You need a space between [ and $var:
Code:
if [ $var = "y" ]
[ is actually a command, so just like ls or cd, you separate it from its arguments with a space.
 
Old 11-21-2010, 06:15 PM   #6
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
Quote:
Code:
read $var
That line also needs to be changed. It should not have the $ in front of the variable. So, it should be:
Code:
read var
Explanation:
The $ tells the shell that you want to substitute the value of the variable in place of the variable's name. You do not use the $ when using a statement that stores a value in the variable. For instance "var=1" or (as above) storing a user's input into the variable.
 
Old 11-21-2010, 06:23 PM   #7
Slightly Disoriented
Member
 
Registered: Sep 2010
Distribution: Ubuntu 10.10
Posts: 87

Original Poster
Rep: Reputation: 2
Mmmm, neither worked. At one point it worked without returning errors with:
Code:
read $var
if [ : var =  "y" ]
But it acted like I didn't press the y key and just went to else.... And when I take out the $ and : it returned the same thing, I also tried removing the "" marks. I though the $ was there to declare that it was a variable, I didn't know I just reconized that it was one.
 
Old 11-21-2010, 06:27 PM   #8
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,819
Blog Entries: 1

Rep: Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200
It should work. Just remove the dollar sign from "read $var" and make sure there's a space in
if [ $var = "y" ]

Can you post the code as it looks now?
 
Old 11-21-2010, 06:27 PM   #9
Slightly Disoriented
Member
 
Registered: Sep 2010
Distribution: Ubuntu 10.10
Posts: 87

Original Poster
Rep: Reputation: 2
Aha! It worked, I only had to take out the $ infront of var in the
Code:
read
line, not the
Code:
if
line. It looked like:
Code:
#!/bin/bash
echo "Yes or No (Y/N)"
read var
if [ $var =  "y" ]
then 
	echo "Yes"
else
	echo "No"
fi
echo "Done"
exit
Now 1 more thing... Is there a way to have more than one if. Like
Code:
echo "Pick a number, 1, 2, 3)
read var
if [ $var = 1 ]
then
echo "You picked 1"
But then have one for 2 and 3.
Thank you for all the help.

Last edited by Slightly Disoriented; 11-21-2010 at 06:29 PM.
 
Old 11-21-2010, 06:30 PM   #10
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,819
Blog Entries: 1

Rep: Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200
You might want to look at Bash's case statement. It'll be more appropriate in this case.
 
Old 11-21-2010, 06:34 PM   #11
Slightly Disoriented
Member
 
Registered: Sep 2010
Distribution: Ubuntu 10.10
Posts: 87

Original Poster
Rep: Reputation: 2
@Sycamorex What do you mean Bash's case statment.
 
Old 11-21-2010, 06:37 PM   #12
barriehie
Member
 
Registered: Nov 2010
Distribution: Debian Lenny
Posts: 136
Blog Entries: 1

Rep: Reputation: 23
Here's a link to a bash scripting guide. Don't be put off by the title.

http://tldp.org/LDP/abs/html/
 
Old 11-21-2010, 06:40 PM   #13
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,819
Blog Entries: 1

Rep: Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200
Quote:
Originally Posted by Slightly Disoriented View Post
@Sycamorex What do you mean Bash's case statment.

http://www.tech-recipes.com/rx/212/b...ase_statement/
 
Old 11-22-2010, 10:49 AM   #14
ee437
LQ Newbie
 
Registered: Oct 2010
Location: Wisconsin, USA
Distribution: Slackware
Posts: 12

Rep: Reputation: 4
looking over your script, without checking in detail, you do need

if [ $var == "y" ] ;

i'm sure you need two ='s, and you probably also need the semicolon ';'.

with only one = sign, it sets var to y, and does not compare.

and, the case statement is very convenient...

i hope that helps.
 
Old 11-22-2010, 11:27 AM   #15
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Quote:
Originally Posted by ee437 View Post
looking over your script, without checking in detail, you do need

if [ $var == "y" ] ;

with only one = sign, it sets var to y, and does not compare.
Did you test your assertion?

When using [, testing if two strings match is performed with =. When using [[, then you need ==. See http://unixhelp.ed.ac.uk/CGI/man-cgi?test.
Quote:
Originally Posted by ee437 View Post
and you probably also need the semicolon ';'.
This is only true if you don't put the then on a new line.
 
  


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
[SOLVED] Bash script to get PID of a specific process. arashi256 Linux - Newbie 6 08-23-2010 08:45 AM
how to get the specific text from a txt file in bash script deepakdeore2004 Programming 8 04-30-2010 07:35 AM
Bash script to search for specific pattern? jimyg10 Programming 22 05-18-2007 02:02 PM
BASH script to copy specific files ScottReed Linux - Software 5 07-20-2006 12:08 PM
bash-script: output text between two ocurrences of a specific string isl01jbe Programming 1 06-17-2004 03:36 PM


All times are GMT -5. The time now is 09:11 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration