LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 10-12-2011, 02:59 PM   #1
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Rep: Reputation: 16
read and if statement in bash


When i hit the y, it still goes to the else statement, and I can't figure out why. I even took out the "sudo /data/bouce light05" statement becasue I thought that was messing it up, but it is not.



Code:
casperh@init01 ~ $ more bounce_the_engine
#!/bin/bash

echo "Do you want to bounce the session"
read $BOUNCEREPLY
if [ "$BOUNCEREPLY" == 'y' ]; then
        echo "Bouncing the CASH engine"
        sudo /data/bounce light05
else
        echo "Not bouncing the CASH engine"
fi
woljoh@ashqoptsinit01 ~ $ ./bounce_the_engine
Do you want to bounce the session
y
Not bouncing the CASH engine
casper@init01 ~ $
 
Old 10-12-2011, 03:05 PM   #2
jthill
Member
 
Registered: Mar 2010
Distribution: Arch
Posts: 211

Rep: Reputation: 67
The comparison operator for test aka [ is '=', not '=='
 
1 members found this post helpful.
Old 10-12-2011, 03:08 PM   #3
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
You need to get rid of $ in "read $BOUNCEREPLY"
 
1 members found this post helpful.
Old 10-12-2011, 03:49 PM   #4
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Original Poster
Rep: Reputation: 16
yes both of you are right - if you only use one equal symbol in bash comparison, if that is the syntax, that is fine, I will study it.
however i don't understand why I have to take the $ sign off the variable for the read function.

is it that you only use the $ when you are expanding variables in bash?
 
Old 10-12-2011, 03:57 PM   #5
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Quote:
Originally Posted by casperdaghost View Post
yes both of you are right - if you only use one equal symbol in bash comparison, if that is the syntax, that is fine, I will study it.
however i don't understand why I have to take the $ sign off the variable for the read function.

is it that you only use the $ when you are expanding variables in bash?
$ is used to RETRIEVE the value of a variable. You don't use it when you assign the value to a variable.
 
Old 10-12-2011, 04:07 PM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Actually = and == are synonyms. Ref: http://tldp.org/LDP/abs/html/comparison-ops.html
 
Old 10-12-2011, 04:18 PM   #7
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019
A single '=' is compatible with traditional bourne shells which may choke on '=='. Good habit to get into using the single form for the sake of portability.
 
Old 10-12-2011, 05:14 PM   #8
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,627

Rep: Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695
Drop the $ please

BOUNCEREPLY=5
Sets variable BOUNCEREPLY to "5"
${BOUNCEREPLY}=6
Now evaluates
5=6

$ can be read as "the value of" in many cases without changing meaning.

read BOUNCEREPLY
reads an input and puts that input into the variable BOUNCEREPLY

read $BOUNCEREPLY should not work at all, but if it did would put the input string into a variable whose name was held by BOUCEREPLY.

Alas, that amount of indirection is not managed well by any current shell.
---------------------------------------------
As for '=' and '==', '=' is generally correct for numeric comparison, while '==' is more correct for strings. Shell programmers have been dealing with users who cannot 'get' that for so long that they have about given up and made the if evaluator handle them through the same code.

It makes that code do the work the programmer should have done, but what else are computers FOR?
 
Old 10-13-2011, 07:26 AM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
If you're using bash then you're really better off using the newer [[ test for strings, and ((..)) for numerical tests. They were designed to avoid a lot of the weaknesses of the old single-bracket test command.

http://mywiki.wooledge.org/BashFAQ/031
http://mywiki.wooledge.org/ArithmeticExpression
 
Old 10-13-2011, 07:39 AM   #10
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Note that unlike "[", which is a synonym for the "test" command, "[[ ]]" and "(( ))" actually are shell syntax.
 
Old 10-13-2011, 08:19 AM   #11
linuxwin2
Member
 
Registered: Oct 2011
Posts: 44

Rep: Reputation: Disabled
Code:
!/bin/bash

echo "Do you want to bounce the session"
read BOUNCEREPLY

if [ $BOUNCEREPLY = "y" ]; then
        echo "Bouncing the CASH engine"
        sudo /data/bounce light05

else
        echo "Not bouncing the CASH engine"
fi
 
Old 10-13-2011, 08:30 AM   #12
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by linuxwin2 View Post
Code:
!/bin/bash

echo "Do you want to bounce the session"
read BOUNCEREPLY

if [ $BOUNCEREPLY = "y" ]; then
        echo "Bouncing the CASH engine"
        sudo /data/bounce light05

else
        echo "Not bouncing the CASH engine"
fi
It's a bad idea to not enclose a variable in quotes in a test command. If the variables contains whitespace, it can cause bad things to happen. Try this:

Code:
echo "Do you want to bounce the session"
read BOUNCEREPLY

if [[ $BOUNCEREPLY = y ]]; then
    echo "Bouncing the CASH engine"
    sudo /data/bounce light05
else
    echo "Not bouncing the CASH engine"
fi
One of the reasons [[]] is better is becuase it properly handles whitespace in variables, even if you don't use quotes.
 
  


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
OR statement in Bash akeenabawa Linux - Software 3 11-14-2008 09:17 AM
Strange if statement behaviour when using bash/bash script freeindy Programming 7 08-04-2008 06:00 AM
bash if statement jstu Programming 7 02-15-2008 07:48 PM
BASH IF statement kinetik Programming 10 05-07-2006 02:48 AM
Bash: Print usage statement & exit; otherwise continue using Bash shorthand operators stefanlasiewski Programming 9 02-07-2006 05:20 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:31 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
Open Source Consulting | Domain Registration