Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 ~ $
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?
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.
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.
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.
!/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
!/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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.