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:
我从服务器获得主机名和ip地址(ubuntu 12.04)。它工作正常。过了一会儿,我意识到服务器在脱机时返回“(none)”作为主机名。现在我试图通过比较主机名和“(none)”来消除这个错误。我尝试了所有这些(*),但没有一个是有效的:
-- 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.
我做错了什么?感谢任何帮助。
3 个解决方案
#1
1
You need a semicolon after ]
后面需要一个分号
if [ ... ]; then ...; else ...; fi
or newlines:
或换行:
if [ ... ]
then
...
else
...
fi
#2
1
This should work;
这应该工作;
hostname="(foo)"
if [ "$hostname" == "(foo)" ]; then echo "equals"; else echo "not equals"; fi
#3
0
Use $HOSTNAME
- my reasoning, which can user to validate other $
variables:
使用$HOSTNAME -我的推理,它可以用户验证其他$ variable:
wilf@comp:~$ echo $hostname
wilf@comp:~$ echo $HOSTNAME
comp
wilf@comp:~$@ wilf@comp:~$ echo $hostname wilf@comp:~$ echo $hostname comp comp wilf@comp:~$
For some reason, $hostname
does not seem to work (Ubuntu 13.10, haven't tried this on other Linuxes)
由于某些原因,$hostname似乎不能工作(Ubuntu 13.10,没有在其他linux上尝试过)
#1
1
You need a semicolon after ]
后面需要一个分号
if [ ... ]; then ...; else ...; fi
or newlines:
或换行:
if [ ... ]
then
...
else
...
fi
#2
1
This should work;
这应该工作;
hostname="(foo)"
if [ "$hostname" == "(foo)" ]; then echo "equals"; else echo "not equals"; fi
#3
0
Use $HOSTNAME
- my reasoning, which can user to validate other $
variables:
使用$HOSTNAME -我的推理,它可以用户验证其他$ variable:
wilf@comp:~$ echo $hostname
wilf@comp:~$ echo $HOSTNAME
comp
wilf@comp:~$@ wilf@comp:~$ echo $hostname wilf@comp:~$ echo $hostname comp comp wilf@comp:~$
For some reason, $hostname
does not seem to work (Ubuntu 13.10, haven't tried this on other Linuxes)
由于某些原因,$hostname似乎不能工作(Ubuntu 13.10,没有在其他linux上尝试过)