df command works in terminal, but not in bash script

1k Views Asked by At

I found the code below on a website. I know what it does and understand it, but I can't get it to work correctly. When I run it in Redhat 6.10, I get an error stating, "line 6: [: /: integer expression expected".

When I run the df code from the CURRENT variable in the terminal it outputs "21". When it's in the script, it outputs "/".

What I'm trying to get it to do is e-mail me if the "/" directory gets over 50%. Any ideas why the df portion of the code is working in the terminal, but not in the script?

#!/bin/bash

CURRENT=$(df / | grep / | awk '{ print $5}' | sed 's/%//g')
THRESHOLD=50

if [ "$CURRENT" -gt "$THRESHOLD" ] ; then
        mail -s 'Disk Space Alert' E-MAIL REMOVED << EOF
Your root partition remaining free space is critically low. Used: $CURRENT%
EOF
fi
1

There are 1 best solutions below

1
AudioBubble On

You might use:

CURRENT=$(df / --output=pcent | tail -n1 | sed 's/%//g')