ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I need to append a line at the bottom of /etc/hosts file to include the current ip address for my pc.
I found the following command that obtains the ip address, but it echo's it to the display and I need to use it as a variable in a shell script in conjunction with the cat command.
Here is the command that echo's the ip address to the display: ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'
You might also want to have a bit more of a look at the awk command as on its own it would be able to complete all the steps where you use grep and cut
Makes little difference here, but consider large complex scripts running on a loaded server and limiting the no. of pipes (and sub shells ) can make all the difference
I tried both the awk & ifconfig examples below and they both give me what I want except the have a leading "addr:" at the beginning of the output line. Is there anyway to clean that up?
If you have more then one interface configured (or alias) you get multiple lines with different IP but same hostname ... you just need to redirect output to /etc/hosts instead of having it shown on standard output.
If you are still getting text in front of the IP address I suspect that your distro is writing something like "inet:192.168.1.4"
so you might haveto work a bit hardet to get the result you want.
Why are you doing this ? do you have a machine that is configuring IP via DHCP and you want to add itself to hosts ?
If so you might want to have an IP reserved on DHCP server so that you get the same one each time and you can do without this thing.
Alternatively you can have the post ip up hook scripts fix the address in hosts for you. This depends on the dhcp client you are using. Look at your client's man page for more details on how you might go about it.
That is the exact command I used in my first post but I added the $HOSTNAME env variable and I'm appending it to /etc/hosts. You should look up the man pages and understand the commands that are being handed to you in this thread. Attempting to filter out the loopback (lo) interface is not very good and you should be using a command similar to what I've given you.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.