Shell Scripting Exercise: count physical RAM in MB

Guys, I'm writing a series of scripts for deploying in our support center. One that I'm working on involves /proc/kcore, the physical RAM of a linux system. I'm trying to display the amount of physical RAM in a system by returning the size o...




Topic Options
#5650 - 02/14/04 09:37 PM Shell Scripting Exercise: count physical RAM in MB
chewmanfoo Offline
stranger

Registered: 02/11/04
Posts: 8
Guys,

I'm writing a series of scripts for deploying in our support center. One that I'm working on involves /proc/kcore, the physical RAM of a linux system. I'm trying to display the amount of physical RAM in a system by returning the size of this file divided by 1024.

I'd like it to eventually say: PHYSICAL RAM: 512MB.

Here's what I've tried, and the results:

Code:
$ echo "System RAM";ls -l /proc/kcore
System RAM
-r--------    1 root     root     958595072 Feb 14 13:13 /proc/kcore
$ 
$ ls -l /proc/kcore | 'expr $5/1024'
/bin/ksh: expr $5/1024: not found
$ ls -l /proc/kcore | echo $5

$ ls -l /proc/kcore | echo $1

$ ls -l /proc/kcore > echo $5
/bin/ksh: cannot create echo: No such file or directory
$ ls -l /proc/kcore | awk "print int($5/1024)"
awk: cmd. line:1: print int(/1024)
awk: cmd. line:1: ^ parse error
awk: cmd. line:1: print int(/1024)
awk: cmd. line:1:            ^ unterminated regexp
awk: cmd. line:2: print int(/1024)
awk: cmd. line:2:                 ^ unexpected newline
$ ls -l /proc/kcore | awk {print int($5/1024)}
/bin/ksh: syntax error: `(' unexpected
$ ls -l /proc/kcore | awk `BEGIN {print int($5/1024)}'
> 
> ^C
$ 
$ ls -l /proc/kcore | awk `{print int($5/1024)}`
/bin/ksh: syntax error: `(' unexpected
$ ls -l /proc/kcore | awk -F `{print int($5/1024)}'
> 
> ^C
$ ls -l /proc/kcore | awk -F `{print int($5/1024)}'
> ^C
$ awk '&#123; print int&#40;$5/1024&#41; &#125;` < ls -l /proc/kcore
> ^C
$ ls -l /proc/kcore > temp; awk '&#123; print int&#40;$5/1024&#41; &#125;` temp
> ^C


As you can see, I'm stuck on awk. Can anyone shed some light on this?

I basically want to take the 5th field of an ls and display the result of that field divided by 1024.

Anyone?

TIA!
chewy

Top
Advertisement
#5661 - 02/15/04 04:09 AM Re: Shell Scripting Exercise: count physical RAM in MB
Maillion Offline
enthusiast

Registered: 01/02/04
Posts: 213
I think there's an error in these two lines:

Code:
$ ls -l /proc/kcore | 'expr $5/1024'
/bin/ksh&#58; expr $5/1024&#58; not found


The AWK stuff is causing problems because these lines are sending AWK invalid data... (GIGO). I hopes this helps but I'm not that familiar with bash scripting, just prgramming... 8)

Top
#5664 - 02/15/04 06:07 AM Re: Shell Scripting Exercise: count physical RAM in MB
Maillion Offline
enthusiast

Registered: 01/02/04
Posts: 213
.
Quote:
'm writing a series of scripts for deploying in our support center. One that I'm working on involves /proc/kcore, the physical RAM
of a linux system. I'm trying to display the amount of physical RAM in a system by returning the size of this file divided by 1024.

I'd like it to eventually say: PHYSICAL RAM: 512MB.

Here's what I've tried, and the results:

Code:
$ echo "System RAM";ls -l /proc/kcore
System RAM
-r-------- 1 root root 958595072 Feb 14 13:13 /proc/kcore
$
$ ls -l /proc/kcore | 'expr $5/1024'
/bin/ksh: expr $5/1024: not found
$ ls -l /proc/kcore | echo $5

$ ls -l /proc/kcore | echo $1

$ ls -l /proc/kcore > echo $5
/bin/ksh: cannot create echo: No such file or directory
$ ls -l /proc/kcore | awk "print int($5/1024)"
awk: cmd. line:1: print int(/1024)
awk: cmd. line:1: ^ parse error
awk: cmd. line:1: print int(/1024)
awk: cmd. line:1: ^ unterminated regexp
awk: cmd. line:2: print int(/1024)
awk: cmd. line:2: ^ unexpected newline
$ ls -l /proc/kcore | awk {print int($5/1024)}
/bin/ksh: syntax error: `(' unexpected
$ ls -l /proc/kcore | awk `BEGIN {print int($5/1024)}'
>
> ^C
$
$ ls -l /proc/kcore | awk `{print int($5/1024)}`
/bin/ksh: syntax error: `(' unexpected
$ ls -l /proc/kcore | awk -F `{print int($5/1024)}'
>
> ^C
$ ls -l /proc/kcore | awk -F `{print int($5/1024)}'
> ^C
$ awk '{ print int($5/1024) }` < ls -l /proc/kcore
> ^C
$ ls -l /proc/kcore > temp; awk '{ print int($5/1024) }` temp
> ^C


As you can see, I'm stuck on awk. Can anyone shed some light on this?

I basically want to take the 5th field of an ls and display the result of that field divided by 1024.


The output of:

ls -l /proc/kcore

is:

-r-------- 1 root root 958595072 Feb 14 13:13 /proc/kcore

The number of column 5 should go into "$5" (apparently a variable) which should then be divided ("/") by 1024, to get
the number of bytes in megabytes. The number in column 5 may not be 'numeric', it may be a string of characters. This is most likely. There
is nothing here to separate column five from all the other columns, nor to determine and/or change column five to a
number and store it in a variable. Some programming languages will allow a character string to be divided by an integer,
but the results will not be valid in a mathematic expression.

this:

$ ls -l /proc/kcore | 'expr $5/1024' takes the output from "ls -l" and sent it through "|" to "'expr $5/1024'"
here is where the error comes in - when the shell executes that command ($ ls -l /proc/kcore | 'expr $5/1024') It
sends all the "ls -l" output to what is on the other side of the "|". It does not extract column 5, nor does it
ensure that the output is valid. When the command is executed you get:

/bin/ksh: expr $5/1024: not found

This is an error from the shell (/bin/ksh) - it could not execute "ls -l /proc/kcore | " because it was unable to locate something
called 'expr $5/1024'. I have never used ksh, but it may be different than bash, but it still seems that 'expr $5/1024' means
simething different to your shell than 'expr $5/1024' means to you. Regardless, because your shell does not have the ouput
of 'expr $5/1024', all the rest of the lines that depend upon the value of column five divided by 1024 will not output that value
to the screen, nor send it through awk.All the awk errors are meaningless, because it has nothing to work with. I hope this helps! 8)

Top
#5688 - 02/16/04 01:35 AM Re: Shell Scripting Exercise: count physical RAM in MB
chewmanfoo Offline
stranger

Registered: 02/11/04
Posts: 8
Yeah, but all variables in the shell are strings.

That's why you can't say:

Code:
$x = 5
$x = $x + 1
echo $x


expr returns the numeric (integer) value of an expression, like "$x + 1"

But I still don't understand why this doesn't work...

help?

Top
#157283 - 02/10/05 12:57 AM Re: Shell Scripting Exercise: count physical RAM in MB
twitten Offline
stranger

Registered: 02/10/05
Posts: 1
Close. Here's the answer:

ls -l /proc/kcore | awk {'print $5/1024'}

[root@corner ~]# ls -l /proc/kcore | awk {'print $5/1024'}
327676

Top


Forums
Windows Support Forums
Everything New Technology
Legacy OS
Hardware
Software
Games
Networking
Customization & Tweaking
Security

Linux Support Forums
Everything Linux
Linux Hardware
Linux Software
Linux Games
Linux Networking
Linux Customization & Tweaking
Linux Security

Apple Support Forums
Everything Apple
Recent Topics
Router keeps disconnecting internet
by rfboyd
12/06/09 04:18 PM
Program Running When Starting Computer
by cbk
12/05/09 11:05 AM
System Crashing Error 1000008e, 1000000a, 0000004e
by PapaPrem
11/30/09 06:01 PM
Starcraft Broodwar Campaign Crash Problem
by Lurker02
11/30/09 12:33 PM
How do I affect the order of ALT-Tab?
by KenJackson
11/28/09 10:10 PM
Who's Online
1 Registered (scubatony), 268 Guests and 40 Spiders online.
Key: Admin, Global Mod, Mod
Forum Stats
91278 Members
24 Forums
58478 Topics
188979 Posts

Max Online: 1079 @ 03/12/08 01:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22