Sivapandian Jayakumar
Sivapandian Jayakumar

Reputation: 133

Linux command to dynamically convert the GB into MB

I need to write a script to show the Total Swap Size, Used Swap Size and % used.

Below are the commands, I'm using.

pgsize=`swapon | tail -1 | awk '{print $3}'`

pgused=`swapon | tail -1 | awk '{print $4}'`

pgpercent=$(($pgused * 100/ $pgsize))

The output of the commands are pgsize = 16G pgused = 22M pgpercent - I'm getting the below error in this line

22M: value too great for base (error token is "22M")

Here, how do I need to convert the 22M into 22 and 16G into 16*1024 and divide it. For eg. (22*100)/(16*1024)

Upvotes: 1

Views: 691

Answers (2)

John1024
John1024

Reputation: 113994

To print the percentage used of the last swap area listed by swapon:

swapon --bytes --show=USED,SIZE | awk 'END{print 100*$1/$2}'

To save it in a variable:

ppgpercent=$(/sbin/swapon --bytes --show=USED,SIZE | awk 'END{print 100*$1/$2}')

Upvotes: 3

akiva
akiva

Reputation: 2737

swapon has a --bytes parameter

hence:

swapon --raw --bytes | tail -1 | awk '{print $4 "/" $3}' | bc

Upvotes: 3

Related Questions