Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am running into an issue that I can't quite figure out. I have a BASH script which recurses through directories and compares the current date to the file-modified time of the file. If it is beyond a certain age, the file gets gzipped. I am receiving the following error:

./serversync.sh: line 87: 1324308130-1323116622
1323581504: syntax error in expression (error token is "1323581504")

The section of code that is running at this point is cited below:

#If the file doesn't have a matching .gz file, compress it
                do if [ ! -e ${FILE}.gz ]
                        then
                                echo "Matching Gzip doesn't exist for $FILE"
                                echo Checking to see if compression needed
#test to make sure that the file is 30 days old, and if it is, gzip
                                FILEMTIME=$(stat -c %Y $FILE)
                                FILEAGE=$(($DATE-$FILEMTIME))
                                echo fileage is $FILEAGE
                                if [ $FILEAGE -gt $COMPRESSWINDOWSTART -a $FILEAGE -lt $COMPRESSWINDOWEND ]
                                        then
                                        echo $FILEAGE is greater than $COMPRESSWINDOWSTART and less than $COMPRESSWINDOWEND
                                        echo Compressing $FILE
                                        gzip $FILE
                                fi
                fi

Line 87 is this line:

                            FILEAGE=$(($DATE-$FILEMTIME))

If anyone can offer any ideas on why this happens id apprciate it!

share|improve this question
1  
I guess $DATE is set before this piece of code? If so please let us know how you set that variable too. You can also try to echo contents of DATE and FILEMTIME before line 87 as well to get more debug information. –  Can Kavaklıoğlu Dec 19 '11 at 15:45
    
I think I found the error on my own after some poking. You are correct, date is set early in the script. It looks like one of the files was rsynced up and had an asterisk, and it was breaking the stat command beforehand. Thank you for looking! –  Matthew Dec 19 '11 at 15:51

3 Answers 3

try

FILEAGE=$DATE-$FILEMTIME

What you had was looking for a variable called "1324308130-1323116622" rather than trying to evaluate the expression.

share|improve this answer

The error ended up being associated with one of the files. A directory had been created by the script which creates files on the local system. It had created a directory in the format alert_*/, which was messing up the stat command. Thank you all for looking!

share|improve this answer

Why don't you use find for this?

find /path/to/searchdirectory -type f -mtime +30 -print0 | xargs -n 1 -0 gzip 

will be a minimum solution to your task, without the error checking.

share|improve this answer
    
It had originally been implemented with find, however we ended up having to switch it over due to issues we were running into (listed in prior questions in here largely). What is listed is a small portion of a much larger script. –  Matthew Dec 19 '11 at 16:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.