Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

420
Views
How to use expr within an if condition

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

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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
over 4 years ago · Santiago Trujillo Report

0

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).

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!