Trying to create an if statement which will search with a username in a location to see if a tar has been done already. If not create said tar in a location. For some reason, my find is running through the then echo command regardless if there is a file in that location or not.
USER_LIST="$(awk '{print $3}' usernamefile.txt)"
for USER_NAME in $USER_LIST;do
echo $USER_NAME
if find /location/to/store/tarfile -type f -iname $USER_NAME;
then
echo "tar file has been found for" $USER_NAME "/location/to/store/tarfile" `date` >> /logfile/log.txt
else
FILE_LOC="$(awk -v $USER_NAME=$3 '{print $5;}' usernamefile.txt)"
tar -czvf ${USER_NAME}.tar.gz /location/to/put/tar/file $FILE_LOC
echo "tar exit code:" $? $USER_NAME "has been archived" `date` >> /logfile/log.txt
fi
done
I'm not sure why but if the find doesn't find anything. Surely it should move onto the else part of the script? the plan is creating tar files such as <username>.tar.gz