I need to create a simple shell script to check filesystems space in a file system called "/arch_nb" then based on the percentage use either run another script or exit.
I was thinking of something simple along the lines of:
df -k | then some action to pipe for percentage used
#!/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
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.
...it looks like my version of AIX (5.2) doesn't like something about the expression "`expr 100 - $used_space`%.\n"?
There's probably something wrong with setting the used space variable in from the df command.
It's good practise to quote your test parameters in double quotes so that if they are not set the test expression will not return with an error and treat the value as null.
I'm not near an AIX box at the moment but suggest you run the following from the command prompt to see what is returned:
#!/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 `$used_space`%.\n"
...but I still get:
run_rman_script_fs_check[7]: } | sed s/%//g: not found.
run_rman_script_fs_check[8]: 0: not found.
Free Space available under "/tmp" is %.
#!/bin/ksh
used_space=0
mount_point=${1:-"/arch_nb"}
threshold=${2:-65}
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
...it turns our that the original statement was returning the header from the "df -k" command in the form of "Iused". This was screwing up the expression in the next line. Thanks to all who helped.
probably best check you've got the right field. I can't remember off hand but df -k might give you free inodes rather than free space hence the Iused header. Try df -I instead and check which field the %free is and adjust your awk statement accordingly.