I have a script that finds files in a folder and deletes them if they are older than 7 days. However, I have a little issue.
#!/bin/bash
BACKUPDIR=/home/vagrant/script/aerospike_backups
TIMESTAMP=$(date +%Y-%m-%d)
LOGPATH=/tmp/logs.txt
ADMINACC=email@example.com
EMEIL=rybka@gl.com
#Let's check existing backups, and if it's older than 7 days delete
find_old () {
if [ -z $(find $BACKUPDIR -mtime +7 -print ) ]
then
return 10
else
find $BACKUPDIR -mtime +7 -delete && echo "Backups deleted at $HOSTNAME on $TIMESTAMP" > $LOGPATH
fi
}
And if I execute this script with empty $BACKUPDIR from terminal use ./scriptname, then type echo $? the shell outputs 10 code as expected because there's not files which is older 7 days or there's just no files at all.
But after I add more if condition like
if [[ $(find_old | echo $?) -gt 0 ]]
then
echo "Script return error code"
else
echo "all is ok"
The script gives me the output all is ok
, but it really shouldn't? What's wrong?