I'm making a bash script which checks the available space on disk:

if [ check_space -gt "85" ]; then
echo "removing"
else
echo "not removing"
fi

check_space returns a number like 52 and the check_space function is:

check_space() {
df /dev/sda1 | tail -1 | awk '{print $5}' | sed 's/%//';
}

It's returning ./backup.sh: line 63: [: check_space: a full expression was expected (I translated it from spanish, so that maybe not exact translation). What could be wrong?

link|improve this question

Please re-run the script with LANG=C so that we get the exact error message. – womble Aug 23 '11 at 13:54
feedback

2 Answers

up vote 10 down vote accepted

Your condition isn't actually calling check_space, you need something like:

if [ `check_space` -gt "85" ]; then
link|improve this answer
@Ryan, no, this example works perfectly well. – glenn jackman Aug 23 '11 at 15:41
You're absolutely right. I'll remove my false doctrine. – Safado Aug 23 '11 at 16:05
feedback

Here's how you can do it...

#!/bin/bash

CHECK_SPACE=`df /dev/sda1 | tail -1 | awk '{print $5}' | sed 's/%//'`;

if [ $CHECK_SPACE -gt "85" ]; then
echo "removing"
else
echo "not removing"
fi

I had to pull out my bash cheat sheet. Functions in bash can't return a value, only an exit status.

link|improve this answer
1  
Usually, you write the function to print information to stdout, and then capture the output when you invoke the function: space=$(check_space) – glenn jackman Aug 23 '11 at 15:40
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.