0

I am writing a simple bash calculator for class and have almost completed it, but I am running into problems with two issues:

  • I am using an if/then argument to try to deal with divide by zero errors. I continue to get an error from this section of the script

  • I use python to return non-integer numbers and it works on all operations except division. I'm not sure why this one specific operation is not working.

I have looked at all the posts it gave me at the top of the screen and I also google queried site:stackoverflow.com for a number of tags and didn't find anything that worked to fix either of these issues. If I'm in the wrong forum or it's already been answered -this is my first time using the site - so I apologize in advance.

Any insight would be most helpful. Thanks in advance!

Here are the sections:

Problem 1: if command returns an error, how would I go about fixing so that it opts out if divide by zero?

if [ $n3 -eq 4 $$ $n2 -eq 0 ]
    then
        echo "Oops, cannot divide by zero. Your answer is undefined."

if

End of first problem.

Problem 2: the below equation does not output non-integer numbers, (e.g. 1 divided by three shows 0, I want it to show 0.333 or similar), why?

    4) 
    quotient=$(python -c "print $n1/$n2")
        echo "The quotient of $n1 and $n2 is $quotient"
            ;;

End of problem area 2.

*)          
        echo "Invalid Option. Pick a number between 1-4 for your operator."
            ;;

esac

  • 2
    For #1 you can use `[ "$n3" -eq 4 ] && [ "$n2" -eq 0 ]` instead of `$$`, but what do you mean by problem 2? What does it output, and what did you expect? (By the way, SO works better with one problem per post. See [One post with multiple questions or multiple posts?](https://meta.stackexchange.com/questions/39223/one-post-with-multiple-questions-or-multiple-posts)) – that other guy Sep 18 '18 at 20:06
  • Thanks that other guy, if I input 1 divide by 3 it gives 0. I want it to provide .333 or equivalent. Kind regards, will – Will McCoy Sep 18 '18 at 20:09
  • Ah ok, instead of "the below equation does output non-integer numbers" you meant "it never outputs non-integer numbers" – that other guy Sep 18 '18 at 20:11
  • Exactly. Sorry should have been more clear. I edited the initial thread. It seems glitchy because as far as I can see it is the same parameters as the other three operations. – Will McCoy Sep 18 '18 at 20:13
  • Consider the "separate post per problem" guidance seconded. Also see [mcve] guidelines: Code in a question should be the *shortest possible code* that actually works (without changes) to demonstrate the problem that is focus of that question. If you can take out the `print`s, or hardcode a specific value instead of using `read`, and the problem still happens, you *should* take them out. – Charles Duffy Sep 18 '18 at 20:21

1 Answers1

1

For your condition, you used [ $n3 -eq 4 $$ $n2 -eq 0 ] where you presumably intended the $$ to be &&. You can do one of:

[ "$n3" -eq 4 ] && [ "$n2" -eq 0 ] # POSIX 
[[ $n3 -eq 4 && $n2 -eq 0 ]]       # Bash specific

For your other problem, see Why does Python return 0 for simple division calculation?. You can do:

n1=1 n2=4
quotient=$(python -c "print $n1.0/$n2")  # Becomes "print 1.0/4"
echo "$quotient"                         # Shows 0.25
that other guy
  • 116,971
  • 11
  • 170
  • 194