1

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)

2 Answers 2

3

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}') 
Sign up to request clarification or add additional context in comments.

Comments

3

swapon has a --bytes parameter

hence:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.