Reputation: 233
I'm getting hostname and ip address from server(ubuntu 12.04) as explained here. It works correctly. After a while, i realize that server returns "(none)" as hostname when it is offline. Now i'm trying to eliminate this error by comparing hostname with "(none)". I tried all of these(*) but none of them works:
-- Getting hostname here
hostname=$(grep "host-name" /var/lib/dhcp/dhclient.${interface}.leases | tail -n1 | cut -d"\"" -f2 | cut -d"." -f1)
-- Trying to compare it with "(none)"
* if [ "$hostname" != "(none)" ] then ... else ... fi
* if [[ $hostname != "(none)" ]] then ... else ... fi
* nouser="(none)" if [ "$hostname" != "$nouser" ] then ... else ... fi
What am i doing wrong ? Thx for any help.
Upvotes: 2
Views: 2842
Reputation: 743
Use $HOSTNAME
- my reasoning, which can user to validate other $
variables:
wilf@comp:~$ echo $hostname
wilf@comp:~$ echo $HOSTNAME
comp
wilf@comp:~$
For some reason, $hostname
does not seem to work (Ubuntu 13.10, haven't tried this on other Linuxes)
Upvotes: 0
Reputation: 18548
This should work;
hostname="(foo)"
if [ "$hostname" == "(foo)" ]; then echo "equals"; else echo "not equals"; fi
Upvotes: 1
Reputation: 12619
You need a semicolon after ]
if [ ... ]; then ...; else ...; fi
or newlines:
if [ ... ]
then
...
else
...
fi
Upvotes: 1