How can I get the hostname of the machine I work on?
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
Solution 1:
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.
Solution 2:
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.