1834150 Members
2389 Online
110064 Solutions
New Discussion

Re: script problem

 
SOLVED
Go to solution
Alfredo_9
Frequent Advisor

script problem

hello,

i have the following script :

#!/bin/sh
set -x
top 1>top.out
cat top.out | egrep '^Memory' | awk ' {print $2 " " $4}' > top2.out
sed 's/M/p' top2.out
rm top.out
exit 0

In order to get the % of free memory, i want to get the total memory and the free memory from the command top. And then I would do some aritmethic operation on it. But I get the following error :

sed: command garbled: s/M/p

any idea ?

thank you
alfredo
7 REPLIES 7
Frederic Sevestre
Honored Contributor

Re: script problem

Hi,

Try with :
sed 's/M/p/' top2.out

Regards,
Fr??d??ric

Crime doesn't pay...does that mean that my job is a crime ?
James R. Ferguson
Acclaimed Contributor

Re: script problem

Hi Alfredo:

Try these changes:

# top -d 1 -f top.out

...the -f option pushes the output to the file.

# sed -e '/M/'p top2.out

...to print the Memory line...

Regards!

...JRF...
Shahul
Esteemed Contributor
Solution

Re: script problem

Hi

U read that sed line once again carefully. U can see an error. U will have to re write like this

sed 's/M/p/g' file

Here g is for global substitution. Anyway U should use / after p.

Best of luck
Shahul
Steve Steel
Honored Contributor

Re: script problem

Hi

use top -f top.out

This gives the top output written clearly

Then work on the file contents.


cat top.out | egrep '^Memory' | awk ' {print $2 " " $4 " "$5 " " $7 " " $8 " "$9}'

steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
Sukant Naik
Trusted Contributor

Re: script problem

Hi Alfredo,

I just refined your script like this

# cat top_script
#!/bin/sh
set -x
top -d 1 -f top.out
cat top.out | egrep '^Memory' | awk ' {print $2 " " $4}' > top2.out
sed 's/M/p/' top2.out
rm top.out
exit 0
Who dares he wins
Rodney Hills
Honored Contributor

Re: script problem

Here is a script that eliminates the need for scratch files as well as reducing it down to one awk process.


#!/bin/sh
set `top -d 1 2>&1 | awk '/^Memory/{sub(/K/,"",$2);sub(/K/,"",$8);print $2 " " $8}'`
echo `expr 100 \* $2 / $1` %

The first line will extract the total and free memory and set it to $1 and $2 respectivelly (via the "set" command).

The second line will calculate the percentage and display the results.
example-
13 %


A perl routine would look nicer, but this shell script will get the job done.

Hope this helps...

-- Rod Hills
There be dragons...
MANOJ SRIVASTAVA
Honored Contributor

Re: script problem

Alferdo


I have another suggsestion I got a script form these forums which cgive the o/p like this :

Memory Stat total used avail %used
physical 32764.0 24296.4 8467.6 74%
active virtual 16899.8 10480.4 6419.4 62%
active real 18039.3 10747.2 7292.1 60%
memory swap 26522.5 19504.4 7018.1 74%
device swap 3332.0 3332.0 0.0 100%

So you can just run this and grep on what ever you want to see . I have attached the uncompiled version with the posting