nir_s
1
Hi guys,
I'm trying to get free space in GB of file system into parameter.
I have the following code:
> cat get_free_space_FS.ksh
#! /bin/ksh
FS=/dw/mar
FreeSpace=`df -h | grep $FS | awk '{print $4}'`
echo $FreeSpace
> ./get_free_space_FS.ksh
362G
My question is ,how can I cut in the "df -h" command the "G" sign from the output , because I want to do manipulations on a numeric result?
Thanks in advance,
Nir
Scott
2
Hi.
Try changing this:
FreeSpace=`df -h | grep $FS | awk '{print $4}'`
To this:
FreeSpace=`df -h | grep $FS | awk '{print $4 + 0}'`
one way, using similar to what you have, and different FS for purposes of example:
# df -h /tmp
Filesystem size used avail capacity Mounted on
swap 78G 670M 77G 1% /tmp
so,
# df -h /tmp |awk 'NR==2{print substr($4,1,length($4)-1)}'
77
using grep has potential to hit multiple matches, so df the FS directly...
HTH
---------- Post updated at 11:11 AM ---------- Previous update was at 11:08 AM ----------
This is nice.... caveat - works for nawk, but not (solaris) awk
pludi
4
df -k | grep $FS | awk '{printf "%d\n",$4/1024/1024}'
Remove a division if you need MiB instead of GiB.
And if you need the free space of a certain mount point you can even shorten it to
df -k $FS | awk '{printf "%d\n",$4/1024/1024}'
nir_s
5
Hi pludi/Tytalus,
Thanks a lot!
very nice solutions.
Best regards,
Nir