LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash - Basic script to check hostname matches (https://www.linuxquestions.org/questions/programming-9/bash-basic-script-to-check-hostname-matches-4175444994/)

scratchyrat 01-10-2013 08:25 AM

Bash - Basic script to check hostname matches
 
Hi,

I'm learning bash and want to write a very basic bit of code to check the server hostname against a variable.

Here is my code

Code:

#!/bin/bash
LIVE=live-server-01

if [ $HOSTNAME != $LIVE ]
then
echo "This is not the Live/Demo server"
else
echo "Server is Live/Demo"
fi

For some reason, the output is always the same however. It always says whatever I have add to the first echo statement, whichever server is is on.

I'm a bit stumped, I thought I was doing quite well until now. Can anyone offer any advice?

Thanks

Snark1994 01-10-2013 08:41 AM

Looks fine to me - I guess the thing to do is put

Code:

echo "[$HOSTNAME]"
in the script and run it on the Live/Demo server - it may be the case that there's whitespace on the end of the hostname, or you might have just mistyped $LIVE.

Sydney 01-10-2013 09:06 AM

You may try and replace $HOSTNAME with `hostname` the back tick tells the bash script to execute the command and use the results in your script.

fur 01-10-2013 04:08 PM

Code:

#!/bin/bash
LIVE=live-server-01
host=$(hostname)
if [ $host != $LIVE ]
then
        echo "This is not the Live/Demo server"
else
        echo "Server is Live/Demo"
fi


scratchyrat 01-11-2013 03:10 AM

Thank you all. I tried but the output was the same, fur's post seemed to work spot on though.

I'm guessing it wasn't quite working because I hadn't handled the hostname variable properly? I thought I didn't have to define it as it was internal (so to speak).

David the H. 01-11-2013 10:44 AM

I can't see any reason why $HOSTNAME should be any different from hostname, unless you're running it remotely or something. Whenever possible the

I'm curious to know what the output is if you echo both of them to the terminal:

Code:

#!/bin/bash

live=live-server-01
host=$( hostname )

echo "hostname = [$host] / HOSTNAME = [$HOSTNAME]"

if [[ $host != $live ]]; then
        echo "This is not the Live/Demo server"
else
        echo "Server is Live/Demo"
fi

You'll notice I made a couple of other code changes too.

1) Scripting With Style

2) When using bash or ksh, it's recommended to use [[..]] for string/file tests, and ((..)) for numerical tests. Avoid using the old [..] test unless you specifically need POSIX-style portability.

http://wiki.bash-hackers.org/commands/classictest
http://wiki.bash-hackers.org/syntax/...nal_expression
http://mywiki.wooledge.org/ArithmeticExpression

scratchyrat 01-14-2013 04:22 AM

Quote:

Originally Posted by David the H. (Post 4867866)
I'm curious to know what the output is if you echo both of them to the terminal:

Seems to work perfectly

Code:

hostname = [demo-server-01] / HOSTNAME = [demo-server-01]
Server is Live/Demo

If I then swap the hostname to a different one (since I can't change it on the server)...

Code:

hostname = [demo-server-01] / HOSTNAME = [demo-server-01]
This is not the Live/Demo server

Quote:

Originally Posted by David the H. (Post 4867866)
You'll notice I made a couple of other code changes too.

1) Scripting With Style

2) When using bash or ksh, it's recommended to use [[..]] for string/file tests, and ((..)) for numerical tests. Avoid using the old [..] test unless you specifically need POSIX-style portability.

http://wiki.bash-hackers.org/commands/classictest
http://wiki.bash-hackers.org/syntax/...nal_expression
http://mywiki.wooledge.org/ArithmeticExpression

Thanks for the advice, I'll read those links and hopefully learn something :)

Snark1994 01-15-2013 06:40 AM

Quote:

Originally Posted by scratchyrat (Post 4869541)
Seems to work perfectly

Code:

hostname = [demo-server-01] / HOSTNAME = [demo-server-01]
Server is Live/Demo

If I then swap the hostname to a different one (since I can't change it on the server)...

Code:

hostname = [demo-server-01] / HOSTNAME = [demo-server-01]
This is not the Live/Demo server


Sorry, you've got the same output for hostname on both servers, but it thinks one is the Live server and one isn't? I don't see how that could work...

Sydney 01-17-2013 07:32 AM

Maybe you could use IP address to verify your location is not or is the live server.


All times are GMT -5. The time now is 11:27 AM.