1

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?

Kevin M
  • 2,312
  • 1
  • 16
  • 21
pmerino
  • 461
  • 2
  • 5
  • 11

3 Answers3

10

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

if [ `check_space` -gt "85" ]; then
JCallicoat
  • 601
  • 5
  • 5
1

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.

Safado
  • 4,786
  • 7
  • 37
  • 54
  • 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
0

"85" with the quotes is a string, not a number... -gt will not work.

suggest:

CHECK_SPACE=$(df -P /dev/sda1 | awk '$1=="/dev/sda1"{sub("%","");print $5}')

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