Try and support more systems without hostname(1)

This commit is contained in:
Roy Marples 2013-06-02 15:18:50 +00:00
parent 9a862f317e
commit 3ea7cb598f

View File

@ -6,17 +6,32 @@ _hostname()
local name=
if [ -z "$1" ]; then
if [ -r /proc/sys/kernel/hostname ]; then
read name </proc/sys/kernel/hostname && echo "$name"
else
if type hostname >/dev/null 2>&1; then
hostname
elif [ -r /proc/sys/kernel/hostname ]; then
read name </proc/sys/kernel/hostname && echo "$name"
elif sysctl kern.hostname >/dev/null 2>&1; then
sysctl -n kern.hostname
elif sysctl kernel.hostname >/dev/null 2>&1; then
sysctl -n kernel.hostname
else
return 1
fi
return $?
fi
if [ -w /proc/sys/kernel/hostname ]; then
# Always prefer hostname(1) if we have it
if type hostname >/dev/null 2>&1; then
hostname "$1"
elif [ -w /proc/sys/kernel/hostname ]; then
echo "$1" >/proc/sys/kernel/hostname
elif sysctl kern.hostname >/dev/null 2>&1; then
sysctl -w "kern.hostname=$1"
elif sysctl kernel.hostname >/dev/null 2>&1; then
sysctl -w "kernel.hostname=$1"
else
# We know this will fail, but it will now fail
# with an error to stdout
hostname "$1"
fi
}