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 I'm a bit stumped, I thought I was doing quite well until now. Can anyone offer any advice? Thanks |
Looks fine to me - I guess the thing to do is put
Code:
echo "[$HOSTNAME]" |
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.
|
Code:
#!/bin/bash |
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 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 |
Quote:
Code:
hostname = [demo-server-01] / HOSTNAME = [demo-server-01] Code:
hostname = [demo-server-01] / HOSTNAME = [demo-server-01] Quote:
|
Quote:
|
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. |