Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

426
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda