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]. 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
Sign up to request clarification or add additional context in comments.

2 Comments

That way the "then" statemant will always be executed... just tried it.
That's just a hint. Anyway the find program is the best way to check those conditions.
1

This works:

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.