Creating a shell script to check filesystem space

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

...place all of this in a logic "if/then" clause?

Anyone have something like that already?

Did you search through the forums ?

You should have something like this - Frustrating Disk space script

Thanks for the link...for some reason the search on this site is not working for me?

Interstingly when I try run the following:

#!/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:

df -k $mount_point | grep % | awk {'print $5'} | sed 's/%//g'

Ah just spotted what the problem might be. Try this (note the single quotes position)

df -k $mount_point | grep % | awk '{ print $5 }' | sed 's/%//g'

Hope that helps.
\

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''
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 %.

I tried changing the line:

used_space="df -k $mount_point | grep % | awk '{print $5}' | sed 's/%//g'"

...as well but with worse results?

When I run the command:

df -k $mount_point | grep % | awk {'print $5'} | sed 's/%//g'

...assuming I assign the value of "/tmp" to "$mount_point" I get:

x-4-1-test# df -k /tmp | grep % | awk {'print $5'} | sed 's/%//g'
Iused
774

...its returning a value (although it contains the "Iused" as well? I get the exact same result when I run the command:

df -k /tmp | grep % | awk '{print $5}' | sed 's/%//g'

...so I dont think the problem is in the qoutes? The problem I think exists in the expression when AIX interprets the "expr" line:

expr 100 - $used_space`%.\n

Got it working with:

#!/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.

:slight_smile:

Good idea...thanks.

you can try

df -k /tmp|grep %|awk 'NR>1{print $3}'|sed 's/%//g'
instead of
df -k /tmp|grep %|awk '{print $5}'|sed 's/%//g'