Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

421
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda