Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If command with user input OS X terminal

So I'm new to the OS X terminal and I'm trying to figure out how to use the if command with the read command.

Like this:

echo stuff:
read f
if [ "f" == "y"]
then 
echo wassup
else exit

What am I doing wrong?

like image 564
Pistachios Avatar asked Sep 21 '13 02:09

Pistachios


1 Answers

You're asking bash to compare whether the strings f and y are equivalent. Clearly, they're not. You need to use a variable substitution:

if [ "$f" == "y" ]

With this, it's asking “is the string consisting of the contents of the variable f equivalent to the string y?”, which is probably what you were trying to do.

You're also missing an fi (if backwards), which ends the if statement. Together:

if [ "$f" == "y" ]
then
    # true branch
else
    # false branch
fi
like image 158
icktoofay Avatar answered Oct 28 '22 00:10

icktoofay