0

I am writing a shell script:

#!/bin/bash

while true
do
     if [[ $# -eq 0 ]] ; then
         echo Enter operand1 value:
         read operand1
 
         # Offer choices
         echo 1. Addition
         echo 2. Subtraction
         echo 3. Multiplication
         echo 4. Division
         echo 5. Exit
 
         echo Enter your choice:
         read choice
 
         echo Enter operand2 value:
         read operand2
 
         # get operands and start computing based on the user's choice
         if [[ $choice -eq 1 ]] ; then
 
 
             echo ----------------------------------------
             echo Addition of $operand1 and $operand2 is $((operand1+operand2))
             echo ----------------------------------------
             echo
         elif [[ $choice -eq 2 ]] ; then
 
             echo ----------------------------------------
             echo Subtraction of $operand1 and $operand2 is $((operand1-operand2))
             echo ----------------------------------------
             echo
         elif [[ $choice -eq 3 ]] ; then
 
             echo ----------------------------------------
             echo Multiplication of $operand1 and $operand2 is $((operand1*operand2))
             echo ----------------------------------------
             echo
         elif [[ $choice -eq 4 ]] ; then
 
             echo ----------------------------------------
             echo Division of $operand1 and $operand2 is $((operand1/operand2))
             echo ----------------------------------------
             echo
         elif [[ $choice -eq 5 ]] ; then
             exit
         else
             echo ----------------------------------------
             echo Invalid choice.. Please try again
             echo ----------------------------------------
             echo
         fi
    
   else
             echo ----------------------------------------
             echo You either passed too many parameters or too less
             echo than the optimum requirement.
             echo
             echo This program accepts a maximum of 2 arguments or no
             echo argument at all in order to run successfully.
             echo ----------------------------------------
   fi
done

I keep getting error message 1")syntax error: operand expected (error token is ".

Would someone be able to help me out with why I am getting this message a possible work around?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578

1 Answers1

0

Based on how the error message is messed up (assuming you did copy it exactly), it looks like you're getting a carriage return from somewhere. If the problematic token was \r1, the error would print as

...syntax error: operand expected (error token is "\r1")

where the CR moves the cursor to the start of the line, so it looks like

...syntax error: operand expected (error token is "
1")

where the second line is actually printed on top of the first.

Check the input your script gets, and see e.g. How to remove carriage return and newline from a variable in shell script

ilkkachu
  • 6,221
  • 16
  • 30