Glam Prestige Journal

Bright entertainment trends with youth appeal.

I am wondering how can I get the hostname (fully qualified domain name) of the machine I am working on?

Here is my script:

 #! /bin/sh hostname=$(host $ipaddr | awk '{print substr($NF,1,length($NF)-1)}') echo $hostname $h= get the current hostname if [ $hostname -ne $h ] then
//instructions fi
6

2 Answers

Use hostname or uname -n to get the kernel hostname (nodename).

hostname -s will give just the first component of the same.

Use hostname -f to get the FQDN – it additionally tries to translate the hostname to an IP address, then back to a domain name.

4

Using an environment variable may be tricky.It might be a matter of the shell you're using: some shells use $HOST, others use $HOSTNAME. I would go with the uname -n option that @grawity mentioned.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy