Disk Space Script to direct output

Hi,

I am working on Sun Solaris 5.10 and want to direct the output from a disk space check script to an output file;

#!/bin/bash
CURRENT=$(df -k /log/logs | grep /log/logs | awk '{ print $5}' | sed 's/%//g')
THRESHOLD=30
if [ "$CURRENT" -gt "$THRESHOLD" ] ; then
echo "Remaining free space is low" > output.txt
else
echo "Space seems ok" > output.txt
fi

It does not seem to be working with the first part and just displays 'space seems ok' while the current space is above the threshold. Any possible hints, indicators would be helpful. Thanks in advance.

start by adding set -x to your script and see what the script does.

Thanks it is really helpful, it seems it is not able to pick the value up from the current

+ + df -k /log/logs
+ grep /log/logs
+ awk { print $5}
+ sed s/%//g
CURRENT=
+ THRESHOLD=30
+ [  -gt 30 ]
+ echo Space seems ok
+ 1> output.txt

I'd suggest debugging df -k /log/logs | grep /log/logs | awk '{ print $5}' | sed 's/%//g' one "pipe" at a time starting from the left...

Thanks, so it seems it does not work after "grep" as it shows below;

+ + df -k /log/logs
CURRENT=Filesystem            kbytes    used   avail capacity  Mounted on
/log             20643785 8189598 12247750    41%    /log
+ + grep /log/logs
+ df -k /log/logs
CURRENT=

This is where it is getting a little confusing, used the same script on another server and it works but instead of directing output to a file it emails instead;

+ + df -k /log/logs
+ grep /log/logs
+ sed s/%//g
+ awk { print $5}
CURRENT=66
+ THRESHOLD=80
+ [ 66 -gt 80 ]

How about...:

df -k /log/logs |awk '$1 ~ "^/log"{ print $5+0}'
df -k /log/logs |awk '$1 ~ "^/log"{ print $5+0}'
awk: syntax error near line 1
awk: bailing out near line 1

Solaris old awk needs

awk '$1 ~ /\//'

Or

awk 'NR>1'

Your two boxes have different file systems, /log/ versus /log/logs/!

on Solaris use nawk instead of awk

1 Like

Thank you very much, this did the trick and now it is working as expected.

+ + df -k /log/logs
+ nawk $1 ~ "^/histlog"{ print $5+0}
+ sed s/%//g
CURRENT=41
+ THRESHOLD=30
+ [ 41 -gt 30 ]
+ echo Remaining free space is low
+ 1> output.txt

you don't need the sed part....