2

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.

4
  • if [ "$hostname" != "(none)" ]; then ... else ... fi Note the ; sign after ] Commented Feb 26, 2014 at 17:28
  • I tried it too. It didn't works. And there is also if [ ] statement without ending ";" in code and it works without a problem. Commented Feb 26, 2014 at 17:31
  • What's the result you're seeing? Commented Feb 26, 2014 at 17:35
  • Replace grep | tail | cut with awk -F'"' '/host-name/ { print $2; exit }' /var/lib/.... Commented Feb 26, 2014 at 17:59

3 Answers 3

1

You need a semicolon after ]

if [ ... ]; then ...; else ...; fi

or newlines:

if [ ... ]
then
   ...
else
   ...
fi
Sign up to request clarification or add additional context in comments.

Comments

1

This should work;

hostname="(foo)"
if [ "$hostname" == "(foo)" ]; then echo "equals"; else echo "not equals"; fi

Comments

0

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)

6 Comments

You are correct about the environment variables, but OP is building a local variable in the script. For some reason - *nix is case sensitive, so $hostname and $HOSTNAME are different variables.
@admdrew - I guess it's sanity. otherwise $iii and $III would be the same variable... unless the Turkish locales are set when i and I would be different letters.
@MaciejPiechotka Not sure what you mean!
Also, HOSTNAME is set at the start of your shell session (if it is set at all), and most likely never updated until you exit that shell and start a new one. Given that the OP is searching DHCP lease files, I think the object is to pick up when the host name changes due to a new lease, which an environment variable won't do unless you go to the effort to update it after every command or something...
@admdrew - "For some reason - *nix is case sensitive" - question if i and I are the same letter depend on your locale. For many locales it's the same letter but in case of tr_TR (AFAIK) they are different. So having case sensitive variables and names in i10n environment is complicated.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.