Tech Support > Operating Systems > UNIX / Variants > Script to check file system space and run a script
Script to check file system space and run a script
Posted by srichards@wss-footwear.com on June 7th, 2006


I have a script that I'm working on to simply check the free space of a
mount and then perform an action based on the results, (i.e. less that
25% left do this...). Here is the script so far:

#!/bin/ksh

used_space=0
mount_point=${1:-"/tmp"}
threshold=${2:-10}

used_space=`df -k $mount_point | grep % | awk {'print $5'} | sed
's/%//g'`
print "Free Space available under \"$mount_point\" is `expr 100 -
$used_space`%.\n"

if [ $used_space -gt $threshold ]
then
print "Space Utilization on \"$mount_point\" has exceeded the
threshhold of $
{threshold}%.\n"
else
print "Free space on \"$mount_point\" is in normal parameters."
fi


....when I run it I get:

expr: 0402-046 A specified operator requires numeric parameters.
Free Space available under "/tmp" is %.

run_rman_script_fs_check[12]: 830: 0403-012 A test command parameter is
not valid.


Apparently AIX 5.2L doesn't like the "expr 100 - $used_space`%.\n"?

Posted by Kyle Tucker on June 7th, 2006


In article <1149701981.628249.294200@g10g2000cwb.googlegroups .com>,
srichards@wss-footwear.com writes:
Works fine with Solaris.

Try this and see if it behaves with AIX.

unused_space=`echo "100 - $used_space" | bc`
print "Free Space available under \"$mount_point\" is $unused_space%.\n"

--
- Kyle

Posted by sdr0303@yahoo.com on June 7th, 2006



Kyle Tucker wrote:

Well that helped a little. I changed the script to:

#!/bin/ksh

used_space=0
mount_point=${1:-"/tmp"}
threshold=${2:-10}

used_space=`df -k $mount_point | grep % | awk {'print $5'} | sed
's/%//g'`
unused_space=`echo "100 - $used_space" | bc`
print "Free Space available under \"$mount_point\" is
$unused_space%.\n"

....and I now get a value for the unused space variable but something is
still off:

syntax error on line 1 stdin
Free Space available under "/tmp" is 774%.


?????


Posted by sdr0303@yahoo.com on June 7th, 2006



sdr0303@yahoo.com wrote:
Thanks to all, ot turned out to be the header of "Iused" being returned

from the "df -k" command. Got it working with:

#!/bin/ksh


used_space=0
mount_point=${1:-"/temp"}
threshold=${2:-10}


used_space=`df -k $mount_point | grep -v "^Filesystem" | awk {'print
$4'} |sed '
s/%//'`
print "Free Space available under \"$mount_point\" is `expr 100 -
$used_space`%.
\n"


if [ $used_space -gt $threshold ]
then
print "Space Utilization on \"$mount_point\" has exceeded the
threshhold of $
{threshold}%.\n"
else
print "Free space on \"$mount_point\" is in normal parameters."
fi


....thanks to all who helped.


Posted by Kevin Collins on June 8th, 2006


In article <1149716383.614663.157320@f6g2000cwb.googlegroups. com>, sdr0303@yahoo.com wrote:

This is a lot of code that is unneccessary. Try:

used_space=`df -k $mount_point | awk '$1 !~ /^Filesystem/ { sub("%$", "", $5); print $5}'`

That reduces 4 commands down to 2 - much better efficiency...

Kevin

--
Unix Guy Consulting, LLC
Unix and Linux Automation, Shell, Perl and CGI scripting
http://www.unix-guy.com


Similar Posts