954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

if-statement help

Need help on this simple script... I am scratching my head...

when I do
echo $myhost
it returns db3

However, when running the script, the first IF condition is always true... what went wrong? Thanks!

Here is the script:

myhost=`hostname -s`

if [ $myhost='db1' ]; then
echo "###############################################"
echo "### Welcome to Site 1 ###"
echo "###############################################"

if [ -f /db2home/db2idev1/sqllib/db2profile ]; then
. /db2home/db2idev1/sqllib/db2profile
fi

elif [ $myhost='db2' ]; then
echo "###############################################"
echo "### Welcome to Site 2 ###"
echo "###############################################"

if [ -f /db2home/db2iuat1/sqllib/db2profile ]; then
. /db2home/db2iuat1/sqllib/db2profile
fi

elif [ $myhost='db3' ]; then
echo "####################################################"
echo "### Welcome to Site 3 ###"
echo "####################################################"

if [ -f /db2home/db2iprd1/sqllib/db2profile ]; then
. /db2home/db2iprd1/sqllib/db2profile
fi

else

echo "exit"
exit 1
fi

walawala
Newbie Poster
2 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

i got it, take the ' ' out of the if condition....
for example
if [ $myhost = db1 ]; then
this works!

walawala
Newbie Poster
2 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

One thing I noticed, too, was that you are using = instead of == . = can assign values to a variable, whereas == checks for equality.

That might have something to do with it, too...

alc6379
Cookie... That's it
Team Colleague
2,820 posts since Dec 2003
Reputation Points: 186
Solved Threads: 147
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You