LinuxQuestions.org
Register a domain and help support LQ
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: 346

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: 209

Rep: Reputation: 66
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,566
Blog Entries: 1

Rep: Reputation: 1026Reputation: 1026Reputation: 1026Reputation: 1026Reputation: 1026Reputation: 1026Reputation: 1026Reputation: 1026
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: 346

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,566
Blog Entries: 1

Rep: Reputation: 1026Reputation: 1026Reputation: 1026Reputation: 1026Reputation: 1026Reputation: 1026Reputation: 1026Reputation: 1026
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
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,508

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Actually = and == are synonyms. Ref: http://tldp.org/LDP/abs/html/comparison-ops.html
 
Old 10-12-2011, 04:18 PM   #7
GazL
Senior Member
 
Registered: May 2008
Posts: 3,425

Rep: Reputation: 936Reputation: 936Reputation: 936Reputation: 936Reputation: 936Reputation: 936Reputation: 936Reputation: 936
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
Member
 
Registered: Apr 2010
Location: USA
Distribution: Debian, Ubuntu, Fedora, RedHat, DSL, Puppy, CentOS, Knoppix
Posts: 778

Rep: Reputation: 173Reputation: 173
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: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948
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: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
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: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
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


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
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


All times are GMT -5. The time now is 07:51 PM.

Main Menu
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