You are getting the error because you have unbalanced if/elses. The syntax of if/else in sh scripts is:
do something else
if needs to be closed by a single
fi. Next, you are storing all files in a single string variable:
files=$(find $filesdir -name '*.txt.gz' -type f -mmin -1)
This means that if your
find command returns something like this:
$ find . -name '*.txt.gz' -type f
Then your variable's contents will be:
$ files=$(find . -name '*.txt.gz' -type f)
$ echo "$files"
But then you check for the existence of a single file with that name:
if [ -f "$files" ]; then
That will never be true since you don't have a file whose name is literally
./file2.txt.gz\n./file1.txt.gz and even if you do, that would also have been included in the results. In any case, you already know they are files and they exist since that is what the
find command was doing, making this test doubly unnecessary.
You also have a lot of unnecessary variables, and you are setting
day_of_month=1 and then using
if [ "$day_of_month" == "1" ] which is pointless since that will always be true. Same for
count. I don't understand what you want to do with those two, so I am guessing you want to set
count to a non-0 value and then exit if the number of files matches that value. I also don't get what the point of the
day_of_month is, I am guessing that you want to delete the files if this is the first day of the month. If so, you also need to check the curent date.
Here's a working version of my best guess for what I think you wanted:
## Get the current day of the month
while true; do
## If this is not the 1st day of the month, just delete the files
## and exit
if [ "$day_of_month" != "1" ]; then
find "$filesdir" -name '*.txt.gz' -type f -mmin -1 -delete
## If this is the first day of the month
## You don't need the file names, only their number,
## so just print a dot so you don't need to worry about
## whitespace in the names
fileCount=$(find "$filesdir" -name '*.txt.gz' -type f -mmin -1 -printf '.\n' | wc -l)
if [[ "$fileCount" == "$expectedFiles" ]]; then
echo "All $expectedFiles files received!"
echo "No data received yet!"
## Wait for 5 seconds. No point in spamming, in fact you
## probably want to sleep for longer.
I suspect this isn't really what you need, but I hope it can serve as a starting point. If you need more help, please ask a new question but give us the details of what the script is supposed to be doing so we can help you better.