user228137
user228137

Reputation: 762

Change hostname bash script

I'm trying to change my hostname at startup of an image to the /24 - /32 IPv4 address. I've come up with the following script, but HNAME remains NULL:

#!/bin/sh
echo "Changing hostname to IP-related"
HNAME=ifconfig | grep 'inet addr:' | grep -v '127.0.0.1' | cut -d. -f4 | awk '{ print $1 }'
echo "Proposed hostname is: $HNAME"
echo

hostname=$HNAME
echo "The new hostname is $HOST_NAME"
echo

Upvotes: 0

Views: 5887

Answers (1)

user228137
user228137

Reputation: 762

I found a solution. Thanks to @anubhava - also fixed a few inconsistencies in my original example:

#!/bin/sh
echo "Changing hostname to IP-related"
HNAME=ifconfig | grep 'inet addr:' | grep -v '127.0.0.1' | cut -d. -f4 | awk '{ print $1 }'
echo "Proposed hostname is: $HNAME"
echo

hostname $HNAME
echo "The new hostname is $HNAME"
echo

Upvotes: 1

Related Questions