Remove % from a variable

I have the following sh file which I originally wrote for HP Unix(which worked great)... but df is different in AIX (HP doesn't have a % in df -k), So I need to remove the % for $x at the moment I'm getting 68% I need it to be 68 so my script complete.

Any help would be welcome.. I'm sure this is a simple thing that slipped my mind..:wall:

#!/bin/ksh
# This script can be used to warn the users that the file system is getting full
#
# Below is set to monitor all the file systems mounted and report to RECEIVER
#
# Usage: as a cron entry for best use.
#set -x
RECEIVER=bja@blank.co.za,gk@blank.co.za,bki@blank.co.za
y=90
# Exclude list of unwanted monitoring, if several partions then use "|" to separate the list
EXCLUDE_LIST="/opt/IBM/TPM|/opt/IBM/SCM|/opt/IBM/ITM"
#LIST=`cat /etc/fstab|grep -v ^#|awk '{print $2}'|grep -vxE "${EXCLUDE_LIST}"`
#LIST=`cat /etc/fstab|grep -v ^#|awk '{print $2}'`
for fs in `df |awk '{ print $NF }'|grep -vxE "${EXCLUDE_LIST}"`
do
     #echo fs=$fs
     x=`df -k $fs |sed -n " p"| awk '{ print $4 }'`
     echo $x
     #echo $y
     if [ $x -gt $y ]
     then
          message="File System $fs on `hostname` is $x% used."
          #echo $subject
          echo $message | mailx -s "`hostname` - File System Full Warning !!!" $RECEIVER
     fi
done
$ echo "85%" | sed 's/%//'
85

---------- Post updated at 12:51 PM ---------- Previous update was at 12:46 PM ----------

you can try this also

 
x=`df -k $fs | awk '{ if(NR==2){print substr($4,length($4)-2,length($4)-1)}}'`

Thanks for responce...

df -k $fs | awk '{ if(NR==2){print substr($4,length($4)-2,length($4)-1)}}'`

nope both your suggestions don't work come back to a >

can you post the output of df -k $fs ( any one mount point )

x=`df -k $fs |sed -n "2 p"| awk '{ print $4 }'| sed 's/%//'`

not probally best practice but it works...thanks

It's not a terrible solution, but it isn't the best it could be. Filtering AWK's input by line number using sed is unnecessary; AWK can do that easily. Ditto for deleting a character from a string.

df -k "$fs" | awk 'NR==2 { sub(/%/, "", $4); print $4 }'

Regards,
Alister

df -k "$fs" | awk 'NR==2 {print int($4) }'

should do the job :rolleyes:

1 Like

Yep. Definitely a much nicer solution. But even that one can be shortened a teensy, weensy, little bit more:

df -k "$fs" | awk 'NR==2 {print $4+0 }'

Regards,
Alister