-2

This is my homework to write calculator on shell script but there is two errors and I couldn't find the solutions.

echo "---------Welcome to Simple Calculator--------"
echo "p=PLUS"
echo "m=MINUS"
echo "x=MULTIPLICATION"
echo "d=DIVISION"
read -p "Enter your choice" ch
if $ch -eq p
then
    echo "Enter Two Number For PLUS"
    read x
    read y
    echo "Sonuç:  $((x+y))"
elif $ch -eq m
then
    echo "Enter Two Number For MINUS"
    read x
    read y
    echo "Sonuç: $((x-y))
elif $ch -eq x
then
    echo "Enter Two Number For  MULTIPLICATION"
    read x
    read y
    echo "Sonuç: $((x\*y))"
elif $ch -eq d
then
    echo "Enter Two Number For DIVISION"
    read x
    read y
    echo "scale=2;x/y" | bc
else
    echo "Stopping calculator"
fi
  • The second "Sonuç: $((x*y)) is missing the closing parentheses. – Mike Waters Mar 13 '17 at 17:42
  • Nope. It is missing a double quote, but the parenthesis are fine – Bruno9779 Mar 13 '17 at 17:45
  • Generally it's helpful to include the error messages you get, or to provide a worked example that triggers the errors. – roaima Mar 13 '17 at 17:58
3

Using a shell syntax checker such as https://www.shellcheck.net/ will help you identify the more obvious syntax errors, such as missing quotes.

However, it won't help with your if ... then clauses, all of which are missing a test operator. Here's an example:

if $ch -eq p

This will fail with some sort of "Command not found" error when you try to run it. (This is why it's helpful to include the output from running a program in the question.)

What you probably meant was this, which uses the test operator [[..]] to perform a string comparison rather than a numeric one.

if [[ "$ch" == p ]]

Finally, it's good practice to double-quote all your variables everywhere you use them, i.e. "$ch" instead of $ch.

  • 1
    Additionally -eq performs an equality test on two integers... It would be better to us a case statement here. – Kusalananda Mar 13 '17 at 18:08
  • @Kusalananda thanks. Case statement? Yes agreed but I assumed the OP hadn't got that far through Scripting 101 yet. – roaima Mar 13 '17 at 18:09
2

In the subtraction section you are missing a " (double quote):

echo "Sonuç: $((x-y))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.