LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-17-2008, 11:35 AM   #1
guif
LQ Newbie
 
Registered: Dec 2008
Posts: 24

Rep: Reputation: 15
Descompose memory result


I use this command to give my free memory in my system: free | grep Mem | awk '{ print $4 }'

And, I would like to know what's the arithmetical math to descompose this result into Gb, Mb and Kbytes.
For example, If I have 1048577Kbyes

1048577 / 1024 = 1024,0009 Mb
1024,0009 / 1024 = 1 Gb

my result: 1GB, 1Mb, 0Kbytes.

I would like to make this with and script.

Help me please!
 
Old 12-17-2008, 03:16 PM   #2
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
To get the quotient and remainder from integer division, try this:
Code:
$ python -c "for x in divmod(8, 3): print x
replace 8 with your awk output and 3 with 1024 and you should have something to work with.
 
Old 12-18-2008, 06:59 AM   #3
guif
LQ Newbie
 
Registered: Dec 2008
Posts: 24

Original Poster
Rep: Reputation: 15
wow! thanks for your answer. but i don't understand...

I have this:

Code:
#!/bin/bash

MEMTOTAL=`free | grep Mem | awk '{ print $2 }'`
MEMUSED=`free | grep Mem | awk '{ print $3 }'`
MEMFREE=`free | grep Mem | awk '{ print $4 }'`

echo "Total: "`expr $MEMTOTAL / 1048576`"Gb"
echo "Used: "`expr $MEMUSED / 1024`"Mb" "($MEMUSED Kb)"
echo "Free: "`expr $MEMFREE / 1024`"Mb" "($MEMFREE Kb)"

Now, how can i added your code?
 
Old 12-29-2008, 12:03 PM   #4
guif
LQ Newbie
 
Registered: Dec 2008
Posts: 24

Original Poster
Rep: Reputation: 15
anybody help me?
 
Old 12-29-2008, 02:38 PM   #5
jcookeman
Member
 
Registered: Jul 2003
Location: London, UK
Distribution: FreeBSD, OpenSuse, Ubuntu, RHEL
Posts: 417

Rep: Reputation: 33
Bash doesn't do floating point precision. So, if you want to do this in pure Bash, you will lose precision. However, you can use awk or bc if you don't want to use Python as already proposed. However, it is much less readable:
Code:
#!/usr/bin/env bash

FREE_BYTES=$(free | grep Mem | awk '{ print $4 }')
FMT=(KB MB GB)

RTR=($(bc <<EOF
/* Two points of floating precision */
scale = 2
free = $FREE_BYTES;
index = 0;
while( free >= 1024 ) {
    free /= 1024;
    index += 1;
}
/* Print converted byte value and index to stdout */
free; index;
EOF
))

echo "Memory free: ${RTR[0]} ${FMT[${RTR[1]}]}"
 
Old 01-14-2009, 11:15 AM   #6
guif
LQ Newbie
 
Registered: Dec 2008
Posts: 24

Original Poster
Rep: Reputation: 15
very thanks jcookeman.
I study this code. Thanks!
 
Old 01-14-2009, 11:29 AM   #7
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
How about the simplest solution of all:

Code:
free -m
free -k
free -b
 
Old 01-14-2009, 11:34 AM   #8
guif
LQ Newbie
 
Registered: Dec 2008
Posts: 24

Original Poster
Rep: Reputation: 15
I see this code now.
I see that I lose precision. Is more easy in Python??
 
Old 01-14-2009, 11:55 AM   #9
jcookeman
Member
 
Registered: Jul 2003
Location: London, UK
Distribution: FreeBSD, OpenSuse, Ubuntu, RHEL
Posts: 417

Rep: Reputation: 33
Quote:
Originally Posted by guif View Post
I see this code now.
I see that I lose precision. Is more easy in Python??
The code that I posted does not lose precision.
 
Old 01-15-2009, 05:44 AM   #10
guif
LQ Newbie
 
Registered: Dec 2008
Posts: 24

Original Poster
Rep: Reputation: 15
oh sorry! I check it wrong with my calc.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
memory test result-can i install FC8......? pradeep2002gs Linux - Newbie 2 12-03-2007 12:54 AM
memory test result-can i install FC8......? pradeep2002gs Linux - Newbie 2 12-02-2007 03:38 AM
Difference between resident memory,shared memory and virtual memory in system monitor mathimca05 Linux - Newbie 1 11-11-2007 04:05 AM
Double memory, what will result? General Linux - Hardware 7 06-25-2006 10:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration