LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   read and if statement in bash (https://www.linuxquestions.org/questions/linux-newbie-8/read-and-if-statement-in-bash-907824/)

casperdaghost 10-12-2011 02:59 PM

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


jthill 10-12-2011 03:05 PM

The comparison operator for test aka [ is '=', not '=='

sycamorex 10-12-2011 03:08 PM

You need to get rid of $ in "read $BOUNCEREPLY"

casperdaghost 10-12-2011 03:49 PM

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?

sycamorex 10-12-2011 03:57 PM

Quote:

Originally Posted by casperdaghost (Post 4496831)
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.

colucix 10-12-2011 04:07 PM

Actually = and == are synonyms. Ref: http://tldp.org/LDP/abs/html/comparison-ops.html

GazL 10-12-2011 04:18 PM

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.

wpeckham 10-12-2011 05:14 PM

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?

David the H. 10-13-2011 07:26 AM

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

MTK358 10-13-2011 07:39 AM

Note that unlike "[", which is a synonym for the "test" command, "[[ ]]" and "(( ))" actually are shell syntax.

linuxwin2 10-13-2011 08:19 AM

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


MTK358 10-13-2011 08:30 AM

Quote:

Originally Posted by linuxwin2 (Post 4497417)
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.


All times are GMT -5. The time now is 04:39 PM.