drifter213
drifter213

Reputation: 109

Shellscript If statement returns error

I am trying to check if a file is older than 5 minutes and if that is the case I want to call another shell script which sends me a mail.

check_file.sh:

#!/bin/sh

if [$(( (`date +%s` - `stat -L --format %Y /home/ftp/test.txt`) > (5*60) ))] = 1
        then sh ./testmail.sh
fi

Error output: 3: ./check_file.sh: [1]: not found

Upvotes: 1

Views: 69

Answers (3)

user1934428
user1934428

Reputation: 22356

Your script first calculates a number, by virtue of the $((....)) expression. In your case, this number seems to be 1.

This means that you are left with the command

if [1] = 1

This means that bash tries to find a command named [1] and invoke it with two parameters, = and 1.

Since no executable file named [1] is found in your PATH, bash tells you that it can not find the file.

I think

if (( (`date +%s` - `stat -L --format %Y /home/ftp/test.txt`) == 1 ))
then 
    ....

should do the job.

Upvotes: 1

drifter213
drifter213

Reputation: 109

This works:

if test "`find /home/ftp/test.txt -mmin +5`"; then
        echo "file found"
fi

Upvotes: 1

hedgar2017
hedgar2017

Reputation: 1657

Try something like:

if find /home/ftp/test.txt -mmin +5 &>/dev/null; then
    <your code>
fi

Upvotes: 1

Related Questions