1

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

1
  • 2
    Add spaces in your if condition: if [ condition ], not if [condition].
    – SLePort
    Commented Jul 30, 2017 at 14:05

3 Answers 3

1

Try something like:

if find /home/ftp/test.txt -mmin +5 &>/dev/null; then
    <your code>
fi
2
  • That way the "then" statemant will always be executed... just tried it.
    – drifter213
    Commented Jul 30, 2017 at 13:56
  • That's just a hint. Anyway the find program is the best way to check those conditions.
    – hedgar2017
    Commented Jul 30, 2017 at 13:58
1

This works:

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

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.

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