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

199
Views
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 answers
Answer question

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 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!