1

I need to send an email only if a condition is reached, but I'm having error running this script:

file='/somewhere/here/file.txt'
value=$(cat "$file")
if [$value < 99]; then
     echo "$value" | mailx -s "title"  me@here.com
fi

the error I'm getting is this:

[line 4: 99]: No such file or directory

file rights: 0755

file '/somewhere/here/file.txt' is present

3

Remember that each programming language has its own syntax and you really should read the relevant documentation before trying to use a new language. In the shell, < doesn't mean "smaller than", it means "take this file as input". To do a numerical comparison, you want -lt for "less than".

In addition, you always need spaces around the [ and ]. So what you wanted to write is something like:

if [ "$value" -lt 99 ]; then
     echo "$value" | mailx -s "title"  me@here.com
fi

For more details, please see help test and man bash.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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