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 of this file divided by 1024.

Linux Customization Tweaking 106 This topic was started by ,



data/avatar/default/avatar05.webp

8 Posts
Location -
Joined 2004-02-11
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/kcoreSystem 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 errorawk: cmd. line:1: print int(/1024)awk: cmd. line:1:            ^ unterminated regexpawk: 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.
 
Anyone?
 
TIA!
chewy

Participate on our website and join the conversation

You have already an account on our website? Use the link below to login.
Login
Create a new user account. Registration is free and takes only a few seconds.
Register
This topic is archived. New comments cannot be posted and votes cannot be cast.

Responses to this topic



data/avatar/default/avatar37.webp

213 Posts
Location -
Joined 2004-01-02
I think there's an error in these two lines:
 

Code:
$ ls -l /proc/kcore | 'expr $5/1024'/bin/ksh: expr $5/1024: 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)


data/avatar/default/avatar37.webp

213 Posts
Location -
Joined 2004-01-02
.
Quote:'m writing a series of scripts for deploying in our support center. One that I'm working on involves /proc/kcore, the physical RAMof 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)


data/avatar/default/avatar05.webp

8 Posts
Location -
Joined 2004-02-11
OP
Yeah, but all variables in the shell are strings.
 
That's why you can't say:
 

Code:
$x = 5$x = $x + 1echo $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?


data/avatar/default/avatar36.webp

1 Posts
Location -
Joined 2005-02-10
Close. Here's the answer:
 
ls -l /proc/kcore | awk {'print $5/1024'}
 
[root@corner ~]# ls -l /proc/kcore | awk {'print $5/1024'}
327676