Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 03:51 PM.
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
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?
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.