LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-10-2013, 08:25 AM   #1
scratchyrat
LQ Newbie
 
Registered: May 2010
Location: United Kingdom
Distribution: Debian, Red Hat, AIX, Ubuntu, Fedora
Posts: 27

Rep: Reputation: 3
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
 
Old 01-10-2013, 08:41 AM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
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.
 
Old 01-10-2013, 09:06 AM   #3
Sydney
Member
 
Registered: Mar 2012
Distribution: Scientific Linux
Posts: 147

Rep: Reputation: 36
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.
 
Old 01-10-2013, 04:08 PM   #4
fur
Member
 
Registered: Dec 2003
Distribution: Debian, FreeBSD
Posts: 310

Rep: Reputation: 35
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
 
1 members found this post helpful.
Old 01-11-2013, 03:10 AM   #5
scratchyrat
LQ Newbie
 
Registered: May 2010
Location: United Kingdom
Distribution: Debian, Red Hat, AIX, Ubuntu, Fedora
Posts: 27

Original Poster
Rep: Reputation: 3
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).
 
Old 01-11-2013, 10:44 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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
 
1 members found this post helpful.
Old 01-14-2013, 04:22 AM   #7
scratchyrat
LQ Newbie
 
Registered: May 2010
Location: United Kingdom
Distribution: Debian, Red Hat, AIX, Ubuntu, Fedora
Posts: 27

Original Poster
Rep: Reputation: 3
Quote:
Originally Posted by David the H. View Post
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. View Post
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
 
Old 01-15-2013, 06:40 AM   #8
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Quote:
Originally Posted by scratchyrat View Post
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...
 
Old 01-17-2013, 07:32 AM   #9
Sydney
Member
 
Registered: Mar 2012
Distribution: Scientific Linux
Posts: 147

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


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Script to change hostname for RHEL wegadnie Linux - General 7 06-22-2011 05:37 AM
Bash script to suppress matches in two column list xsyntax Linux - General 6 09-03-2010 05:08 AM
Basic Bash Script help mpmackenna Linux - Newbie 3 03-18-2009 10:23 AM
Basic Bash script help valadamir Programming 2 12-01-2004 10:37 AM
bash: routine outputting both matches and non-matches separately??? Bebo Programming 8 07-19-2004 06:52 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration