Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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 want to write a script in ksh that will print the comma delimited list of Host,IP,FreeMem,averageLoad.
This is what i tried and i get lost:
cd /etc
host=`cat HOSTNAME`
cd ..
I_P=`/sbin/ifconfig`
IP=${I_P#*eth0 * inet addr:} # delete everything to the left of inet addr:
IP=${IP%% *} # delete from the right till you find the last value that variable IP now holds
echo "Host, IP"
echo $host , $IP
the above script acheives to get the Host and the IP address. Please help with the remaining elements as to how i can get them. your assistance is much appreciated. thanks!
play around with the value for 2 above note the (`) they are tilda not commas. Good luck
sorry cant be of too much help aint a guru, I myself experiment and mess around a little before I come up with real scripts and am not not my box right now.
and, makuyl is it possible to use another method except using awk as in you reply post: echo `free -t|awk 'NR==5 {print $4}'`. Or could you at least explain to me in a nutshell as to what the statement actually means. I haven't got the grips of AWK as yet. Please...
The hostname command with the -s option prints out the short hostname, that is without the domain name. Strip out the -s option if you want the complete hostname. To extract the data (or better to remove the unwanted parts) from the ifconfig and free output I used sed in alternative to awk or parameter substitution. Note that the total free memory is comprehensive of the swap memory.
The hostname command with the -s option prints out the short hostname, that is without the domain name. Strip out the -s option if you want the complete hostname. To extract the data (or better to remove the unwanted parts) from the ifconfig and free output I used sed in alternative to awk or parameter substitution. Note that the total free memory is comprehensive of the swap memory.
thanks colucix
i really wish to understand what the following means and right now i don't. can you please break it down for me: '/Total/{s/.* //;p}'.
Another thing is that I want total memory and not only free memory. thanks
from this output you want to extract the total memory from the last line. In my example I extracted just the last field "2368". So I tell to sed: for each line containing "Total" remove anything till the last blank space and print out the rest:
Code:
sed -n '/Total/{s/.* //;p}'
where /Total/ is the sed address (the commands inside brackets are executed only for those lines that match the regular expression). The command is the substitution of any number of characters .* followed by a space with nothing (that is remove this part of the line). Take in mind that a dot matches any single character and the asterisk means any number (zero included) of the preceding expression. The p command (separated by semi-colon) is to print what remains of the line after the substitution.
If you want the total memory, swap included you can do something like
Code:
free -tm | sed -n '/Total/{s/Total: *//;s/ .*//;p}'
that is first remove the first part containing Total and the following blank spaces, then remove the last part from the first blank space to the end. Or just simply use awk
Code:
free -tm | awk '/Total/{print $2}'
if you're interested in the physical memory only, just change Total with Mem in the commands above. Or just parse the content of /proc/meminfo (dividing by 1000 if you want the output in Mb)
from this output you want to extract the total memory from the last line. In my example I extracted just the last field "2368". So I tell to sed: for each line containing "Total" remove anything till the last blank space and print out the rest:
Code:
sed -n '/Total/{s/.* //;p}'
where /Total/ is the sed address (the commands inside brackets are executed only for those lines that match the regular expression). The command is the substitution of any number of characters .* followed by a space with nothing (that is remove this part of the line). Take in mind that a dot matches any single character and the asterisk means any number (zero included) of the preceding expression. The p command (separated by semi-colon) is to print what remains of the line after the substitution.
If you want the total memory, swap included you can do something like
Code:
free -tm | sed -n '/Total/{s/Total: *//;s/ .*//;p}'
that is first remove the first part containing Total and the following blank spaces, then remove the last part from the first blank space to the end. Or just simply use awk
Code:
free -tm | awk '/Total/{print $2}'
if you're interested in the physical memory only, just change Total with Mem in the commands above. Or just parse the content of /proc/meminfo (dividing by 1000 if you want the output in Mb)
and, makuyl is it possible to use another method except using awk as in you reply post: echo `free -t|awk 'NR==5 {print $4}'`. Or could you at least explain to me in a nutshell as to what the statement actually means. I haven't got the grips of AWK as yet. Please...
That one's easy to explain. I told awk to print the fourth column of the fifth line.
I didn't mean you should use those echo lines as I wrote them, just wanted to give you some ideas for a possible solution. Much cleaner to use variables like you and colucix already did.
On my system it does not display the IP of the eth0 interface, but that one of the localhost, e.g. 127.0.0.1
It should return whatever the hostname resolves to. It works correctly on my system. I think some distros put the hostname on the loopback entry in /etc/hosts in order to ensure that it can always be resolved, especially if they're using a dhcp setup to get an address, so yes, on reflection, that's something to watch out for. Good spot Colucix.
It's an interesting problem. If you can't rely on 'hostname -i', then you have to parse the output of ifconfig, but then how do you decide which interface to report should you encounter a system with multiple interfaces.
It's an interesting problem. If you can't rely on 'hostname -i', then you have to parse the output of ifconfig, but then how do you decide which interface to report should you encounter a system with multiple interfaces.
Good point. Apparently you have to know a priori which interface to rely on. Unless there is a way to know which is the external interface and which is (are) the internal one(s), other than check the address itself after its extraction, of course. Moreover, there are a lot of different types of network interface. For example having my system at home connected via DSL modem and running OpenSuse I have
Code:
dsl0 Link encap:Point-to-Point Protocol
eth0 Link encap:Ethernet
lo Link encap:Local Loopback
wlan0 Link encap:Ethernet
wmaster0 Link encap:UNSPEC
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.