2

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

Noomak
  • 279

1 Answers1

4

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.

Kusalananda
  • 333,661
terdon
  • 242,166