I'm trying to do use the return for a function in an if statement within bash.
I've come up with this very simple example which unfortunately fails.
always_false () {
return 1
}
always_true () {
return 0
}
if [ always_false ]
then
echo 'it is true'
else
echo 'it is false'
fi
if [ always_true ]
then
echo 'it is true'
else
echo 'it is false'
fi
Returns
it is true
it is true
I would expect it to return
it is false
it is true
The actual example of what i'm trying to achieve is checking the disk space for a particular directory. But I thought the simpler example would be better for understanding where i'm going wrong.
check_space () {
spaceAvailable=$(($(stat -f --format="%a*%S" /path/to/dir)))
fiftyGig=50000000000
if [ $spaceAvailable -gt $fiftyGig ]
then
return 0
else
return 1
fi
}
if [ check_space ]
echo 'do some things here'
else
echo 'not enough disk space'
fi
I found another answer that suggested I should be doing something like this:
always_false () {
[[ 0 -gt 1 ]]
return
}
always_true () {
[[ 2 -gt 1 ]]
return
}
But this did not seem to work and always came up true.