[SOLVED] Bash - Basic script to check hostname matches
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
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.
Distribution: Debian, Red Hat, AIX, Ubuntu, Fedora
Posts: 27
Original Poster
Rep:
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).
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.