[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Getting hostname

Holger Kubiak wrote:
> If the package host is installed I get:
> holger@myhost:~$ host
> Name: www.heise.de
> Address:
> If the package bind9-host is installed I get:
> holger@myhost:~$ host
> domain name pointer www.heise.de.
> I wanted to grep and awk the output. As you can see this will be
> difficult.

Not too difficult.  I have been using the following in a script which
I needed for similar reasons.

  host | awk '/Name:/||/domain name pointer/{print$NF;exit}'

  www.heise.de     # host
  www.heise.de.    # bind9-host

The only difference is that bind9-host will leave a dot on on the end
of the name.  Depending upon your needs you need to be aware of that
possibility.  Assuming 'h' is a variable holding this name then
'h=${h%.}' should ensure no trailing dot regardless of which host is

I rather like the 'dig' solutions posted by others.  I really just
posted this because I wanted to share the simple awk solution.  But
now I am thinking seriously of converting to the dig solution myself. :-)


Attachment: pgp9JShI2r9AB.pgp
Description: PGP signature

Reply to: