I have the following if condition:
STATUSFILE = "foo.status"
NEWTIME=1389253927
if [ -f $STATUSFILE ] && [ expr $NEW_TIME - cat $STATUSFILE -gt 3600 ]; then
echo "foo"
fi;
and I get [: too many arguments
The argument of if is a unix command. [ is equivalent to test. The result of if depends on the return status of that command.
Therefore, if you want to test the return status of a command, you should remove [.
When you want to check the output of a command (not its status), you have to keep [ and put the command in $( and ).
Your statement should be rewritten as:
if [ -f $STATUSFILE ] && [ $(expr $NEWTIME - $(cat $STATUSFILE)) -gt 3600 ]; then
Better than that is to use the arithmetic capabilities of bash, provided by $(( and )):
if [ -f $STATUSFILE ] && [ $(($NEWTIME - $(cat $STATUSFILE) )) -gt 3600 ]; then
You use foo $(expr 1 + 1) to use the output from the command expr 1 + 1 as the argument for the command foo. The command foo expr 1 + 1 simply runs foo with the four literal strings expr, 1, +, and 1 as its arguments (and I cannot imagine a sane, unambiguous way for it to mean anything else).