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

200
Visualizações
Why doesn't my if statement work in shell script?

I'm working on a simple game that I'm programming in shell for a college assignment and I don't know why my last if doesn't work properly:

#! /bin/bash

sort -n num.txt > sorted_nums.txt

pc_num=`head -1 sorted_nums.txt`

read $user_num

if [ $user_num = $pc_num ]
then
      echo "You are correct"
else
      echo "You're wrong, the right answer was $pc_num"
fi

The problem comes in the if statement because when I run the programme it will always print the error:

./nums.sh: line 9: [: =: binary operator expected

I have also tried replacing = with -eq and still doesn't work

And then print:

You're wrong, the right answer was $pc_num

Thank you!

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Well, you are being directed to the wrong line. Actually you are reading the user input wrong. It must be read user_num without the $.

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