I am having an issue with the code below where it executes the array and if statement block to check to see if the file is in active use or not. In order to do the debugging, I have run the script with -x and it is missing the value when it compares the two values in [ … ]. I have searched through number of websites to find the answers but have not been able to do so. What would be the problem or fix over here for that block to compare if the file is in active use or not which will going to be deleted?
#!/bin/bash
#---- parameters check----#
if ( [ "$2" -le "0" ] )
then
echo 'Invalid number of days...'
echo ''
echo 'The number of days must be greater then 0'
exit 1;
fi
#----variables----#
Days="$2"
timestamp=$(date +%Y%m%d_%H%M%S)
FILEFOLDER="$1"
logfile=~/"log_purge_$timestamp.txt"
#----foldercheck----#
if [ -d $FILEFOLDER ]
then
echo "File Folder Directory $FILEFOLDER is set." | tee "$logfile"
else
echo "File Folder Directory $FILEFOLDER does not exist! Process will
Exit."
exit 1;
fi
#----Find if the file is In Use through Array and IF---#
files_check=($(find "$FILEFOLDER" -type f -mtime +"$Days" -print))
for f_c in "${files_check[@]}"
do
if [ "${f_c}" == "$( lsof | grep "$f_c")" ]
then
echo "$f_c is in use rite now,quiting the process"
exit 1
fi
done
echo "No file is in Use right now. Safe to remove"
#---Total Number of Files & Purge ---#
files_num=$(find "$FILEFOLDER" -type f -mtime +"$Days" | wc -l)
echo "Total number of files" $files_num | tee -a "$logfile"
find "$FILEFOLDER" -type f -mtime +"$Days" -print | sed "s|$FILEFOLDER||g" | tee -a $logfile
echo "Purging the files now ....."
find "$FILEFOLDER" -type f -mtime +"$Days" -exec rm {} \;`
()around[ "$2" -le "0" ][ $2 -le 0 ]or(( $2 < 0 ))