0

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
1
  • 1
    HNAME=$(ifconfig | grep 'inet addr:' | grep -v '127.0.0.1' | cut -d. -f4 | awk '{ print $1 }') Commented Mar 19, 2015 at 19:44

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

The verification of the hostname in the 2nd to last line will not work, rather use echo $HOSTNAME otherwise it'll just echo the variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.