Error with my shell scripting

Need some help with a shell scripting.
This is my code:

while true
do
  echo "1. Add"
  echo "2. Subtract"
  echo "3. Multiply"
  echo "4. Divide"
  echo "5. Quit"

  read -p "Enter your choice: " choice

  if [ $choice -eq 5 ]
  then
    break
  fi

  number1=$1
  number2=$2

  read -p "Enter first number: " $number1
  read -p "Enter second number: " $number2

  if [ $choice -eq 1 ]
  then
    echo Answer=$(( $number1 + $number2 ))
  elif [ $choice -eq 2 ]
  then
    echo Answer=$(( $number1 - $number2 ))
  elif [ $choice -eq 3 ]
  then
    echo Answer=$(( $number1 * $number2 ))
  elif [ $choice -eq 4 ]
  then
    echo Answer=$(( $number1 / $number2 ))
  else
    continue
  fi
done

And I receiving this error message:

bob@caleston-lp10:~$ ./calculator.sh

  1. Add
  2. Subtract
  3. Multiply
  4. Divide
  5. Quit
    Enter your choice: 1
    Enter first number: 2
    Enter second number: 3
    ./calculator.sh: line 24: + : syntax error: operand expected (error token is "+ ")

I’m trying to figure out what it’s wrong but I don’t see anything wrong with my code, what is the shell scripting concept I’m missing?

Anybody can help?

Thanks

Hello @Richardbmk,
Try this

while true
do
  echo "1. Add"
  echo "2. Subtract"
  echo "3. Multiply"
  echo "4. Divide"
  echo "5. Quit"

  read -p "Enter your choice: " choice

  if [ $choice -eq 5 ]
  then
    break
  fi

  number1=$1
  number2=$2

  read -p "Enter first number: " number1
  read -p "Enter second number: " number2

  if [ $choice -eq 1 ]
  then
    echo Answer=$((number1+number2))
  elif [ $choice -eq 2 ]
  then
    echo Answer=$((number1-number2))
  elif [ $choice -eq 3 ]
  then
    echo Answer=$((number1*number2))
  elif [ $choice -eq 4 ]
  then
    echo Answer=$((number1/number2))
  else
    continue
  fi
done

It looks like now it is working.

The only difference is that you take out the white spaces in these statements right?

$((number1/number2))

Hello @Richardbmk,
Yes, also when you need to read from a user, you need to follow this format read variable not read $variable

Thanks! :slight_smile:

Now I understand.

You could also do it like this. Much nicer than the nasty if ... elif ... elif

while true
do
  echo "1. Add"
  echo "2. Subtract"
  echo "3. Multiply"
  echo "4. Divide"
  echo "5. Quit"

  read -p "Enter your choice: " choice

  if [ $choice -eq 5 ]
  then
    break
  fi

  number1=$1
  number2=$2

  read -p "Enter first number: " number1
  read -p "Enter second number: " number2

  case $choice in
  1)
    echo Answer=$((number1+number2))
    ;;
  2)
    echo Answer=$((number1-number2))
    ;;
  3)
    echo Answer=$((number1*number2))
    ;;
  4)
    echo Answer=$((number1/number2))
    ;;
  *)
    continue
    ;;
  esac
done

Thanks for this one jajaja

I have a similar question during the labs. Very helpful

Hi @Richardbmk
please ensure division (smaller number / bigger number) works for you?
you may refer this:

controlplane $ cat calculator.sh
while true
do
echo “1. Add”
echo “2. Subtract”
echo “3. Multiply”
echo “4. Divide”
echo “5. Quit”

read -p "Enter your choice: " choice

if [ $choice -eq 5 ]
then
break
fi

#number1=$1
#number2=$2

read -p "Enter first number: " number1
read -p "Enter second number: " number2

if [ $choice -eq 1 ]
then
echo Answer=$((number1+number2))
elif [ $choice -eq 2 ]
then
echo Answer=$(( number1 - number2 ))
elif [ $choice -eq 3 ]
then
echo Answer=$(( number1 * number2 ))
elif [ $choice -eq 4 ]
then
#echo Answer=$(( number1 / number2 ))
Answer=echo $number1 / $number2 | bc -l
printf “%.2f\n” $Answer
else
continue
fi
done
controlplane $

Thanks

1 Like

@unnibb208 that is a really good point that I didn’t even notice.

I’ll improve the code with your code.

Many Thanks!

putting set -vx on the first line of script may help you debug the script as it runs.

can you provide an example?

It will something like this:

cmd$~: ./my-calculator.sh -vx

Thanks in advance for all the help :slight_smile:

vi calculator.sh → :1 → Shift+o → set -vx → Save and exit.