Hey friends. I've been bashing my head against this one for a while, including doing a lot of googling and searching of StackOverflow. I'm hoping one of you kind folks can help with some expertise.
I have an "If" statement containing a "Find" statement within it, which works perfectly as long as the file being "found" doesn't reside a folder that contains spaces in the path name. As soon as spaces get in the mix, then it gives me an error: "Unary Operator Expected".
Here is an example script. There are two versions of the testFile variable. Depending on which one I uncomment, the script either works as expected, or it throws "Unary Operator Expected". (Note: A copy of the file exists in both places for the purposes of this test, both in the unspaced location and the spaced location.)
I've been trying various quoting options for the variable and none are working to improve the script. I've read through
this but I must be missing something. Also, this may be a factor: I'm doing this at the Mac OS X shell, which has Bash version 3 instead of version 4.
Any ideas of what the correct syntax of that "find" statement should be?
#!/bin/bash
# This works as expected:
testFile="/Users/tonyfabris/Documents/test-file"
# This produces the error "Unary Operator Expected"
# when it hits the "find" command because of the
# space in the path name:
testFile="/Users/tonyfabris/Documents/Test Folder/test-file"
# Ensure the file is present. Works with both versions of the files.
if [ ! -f "$testFile" ]
then
echo "Test file not found. Exiting."
exit 1
fi
echo "Test file is present. Continuing program."
# Test the age of the file and only perform an operation
# if the file is more than 18 hours old.
if [ $( find "$testFile" -mmin +1080 ) ]
then
echo "File is more than 18 hours old. Continuing with program."
else
echo "No action will be performed based on the file age."
exit 1
fi
echo "Performing action on the file."