Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|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

2 Answers 2

up vote 10 down vote accepted

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

if [ `check_space` -gt "85" ]; then
share|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

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.

share|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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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