[Solved] pls help, what's wrong with my shell script...
I read PaulLove and JoeMerlino's Book(<<Beginning Unix>>)
and copied this script with vi below(1stscript.sh):
Code:
#!/bin/bash
echo "GUESS THE SECRET COLOR"
read COLOR
if [ $COLOR="PURPLE" ]
then
echo "CORRECT!"
else
echo "INCORRECT!! THE SECRET COLOR IS $COLOR"
fi
then i run it and got this result:
Quote:
$ chmod +x 1stscript.sh
$ ./1stscript.sh
GUESS THE SECRET COLOR
ABC
CORRECT!
WHAT?!!!
....
is there any thing wrong with my script.
my platform is Ubuntu Server 64bit.
pls help, thanx!
Re: pls help, what's wrong with my shell script...
the if statement needs a ; and it's good practice to place your then at the end
#!/bin/bash
echo "GUESS THE SECRET COLOR"
read COLOR
if [ $COLOR="PURPLE" ] ; then
echo "CORRECT!"
else
echo "INCORRECT!! THE SECRET COLOR IS $COLOR"
fi
does that work?
Re: pls help, what's wrong with my shell script...
doesn't work correctly, it returened the same result...
is there any way to solve this problem?...
Re: pls help, what's wrong with my shell script...
Quote:
Originally Posted by
koori
I read PaulLove and JoeMerlino's Book(<<Beginning Unix>>)
and copied this script with vi below(1stscript.sh):
Code:
#!/bin/bash
echo "GUESS THE SECRET COLOR"
read COLOR
if [ $COLOR="PURPLE" ]
then
echo "CORRECT!"
else
echo "INCORRECT!! THE SECRET COLOR IS $COLOR"
fi
then i run it and got this result:
WHAT?!!!
....
is there any thing wrong with my script.
my platform is Ubuntu Server 64bit.
pls help, thanx!
what if you define the secret color first
#!/bin/bash
secret=PURPLE
echo "GUESS THE SECRET COLOR"
read COLOR
if [ $COLOR = $secret ] ; then
echo "CORRECT!"
else
echo "INCORRECT!! THE SECRET COLOR IS $secret"
fi
Re: pls help, what's wrong with my shell script...
*facepalm*
I must be getting sleepy lol
1 Attachment(s)
Re: pls help, what's wrong with my shell script...
Quote:
Originally Posted by
clasificados
what if you define the secret color first
#!/bin/bash
secret=PURPLE
echo "GUESS THE SECRET COLOR"
read COLOR
if [ $COLOR = $secret ] ; then
echo "CORRECT!"
else
echo "INCORRECT!! THE SECRET COLOR IS $secret"
fi
nothing has changed...
well, does that mean my shell was broken?
Re: pls help, what's wrong with my shell script...
You just needed to put a space around the =:
Code:
#!/bin/bash
echo "GUESS THE SECRET COLOR"
read COLOR
if [ $COLOR = "PURPLE" ]
then
echo "CORRECT!"
else
echo "INCORRECT!! THE SECRET COLOR IS $COLOR"
fi
When doing comparisons, it is a good habit to use quotes around the variable:
Code:
if [ "$COLOR" = "PURPLE" ]
Also why say that the secret color is $COLOR when $COLOR is the users response?
Code:
GUESS THE SECRET COLOR
asdf
INCORRECT!! THE SECRET COLOR IS asdf
;)
Also, there is a programming section in the forums:
Ubuntu Forums > The Ubuntu Forum Community > Other Community Discussions > Development & Programming > Programming Talk
This would be a good place to get help with this sort of stuff.
Re: pls help, what's wrong with my shell script...
Quote:
Originally Posted by
lavinog
You just needed to put a space around the =:
Code:
#!/bin/bash
echo "GUESS THE SECRET COLOR"
read COLOR
if [ $COLOR = "PURPLE" ]
then
echo "CORRECT!"
else
echo "INCORRECT!! THE SECRET COLOR IS $COLOR"
fi
When doing comparisons, it is a good habit to use quotes around the variable:
Code:
if [ "$COLOR" = "PURPLE" ]
Also why say that the secret color is $COLOR when $COLOR is the users response?
Code:
GUESS THE SECRET COLOR
asdf
INCORRECT!! THE SECRET COLOR IS asdf
;)
Also, there is a programming section in the forums:
Ubuntu Forums > The Ubuntu Forum Community > Other Community Discussions > Development & Programming > Programming Talk
This would be a good place to get help with this sort of stuff.
Thanx, the problem is solved and thank you for guiding me to the right place:)
but there is still one strange thing happened.
after i entered "./1stscript.sh" and enter an incorrect value it often return an error message below:
Quote:
$ ./1stscript.sh
GUESS THE SECRET COLOR
BLUE
./1stscript: line 7:[: =:] unary operator expected
YOUR GUESS WAS WRONG. THE SECRET COLOR IS <PURPLE>.
but if i output this message into file the error message was dissappeared:
Quote:
$ ./1stscript.sh > error.log
GUESS THE SECRET COLOR
YOUR GUESS WAS WRONG. THE SECRET COLOR IS <PURPLE>.
why...okay maybe i could bring this question into that place.
thank you:)
Re: pls help, what's wrong with my shell script...
Can you post the contents of the script you used?