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 |
The comparison operator for test aka [ is '=', not '=='
|
You need to get rid of $ in "read $BOUNCEREPLY"
|
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? |
Quote:
|
Actually = and == are synonyms. Ref: http://tldp.org/LDP/abs/html/comparison-ops.html
|
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.
|
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? |
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 |
Note that unlike "[", which is a synonym for the "test" command, "[[ ]]" and "(( ))" actually are shell syntax.
|
Code:
!/bin/bash |
Quote:
Code:
echo "Do you want to bounce the session" |
All times are GMT -5. The time now is 04:39 PM. |