bash script of a simple calculator

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lifeisgreat20009
    New Member
    • Oct 2007
    • 70

    #1

    bash script of a simple calculator

    Code:
    Echo “Enter first number:”
    Read n1
    Echo “Enter Second number:”
    Read n2
    Echo “Enter operation to be carried out that is +, - , / or X”
    Read opr
    If [ opr==”+” ]
    Then
    echo $(( $a + $b ))
    elif [ opr==”-“]
    then
    echo $(( $a - $b ))
    elif [ opr==”/” ]
    then
    echo $(( $a / $b )) 
    elif [ opr==”X” ]
    then
    echo $(( $a * $b ))
    fi
    Is the script above correct syntactically ?? its not working fine. I searched net for syntax errors but all seems to be correct. Plz tell me where modifications are needed in the code ?
    thnx and regards
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    You're entering n1 and n2 and then operating on a and b

    Comment

    • lifeisgreat20009
      New Member
      • Oct 2007
      • 70

      #3
      otherwise is it ok ??
      I made changes here only by mistake..
      Its a and b only in the original script

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Sorry I'm not a bash expert it is just appeared to be an obvious mistake in any programming language.

        Comment

        • gpraghuram
          Recognized Expert Top Contributor
          • Mar 2007
          • 1275

          #5
          Hi,
          There are some couple of errors in the code .
          check this
          Code:
          echo "Enter first number:" 
          read a 
          echo "Enter Second number:" 
          read b 
          echo "Enter operation to be carried out that is +, - , / or X" 
          read opr 
          #echo "opr =$opr"
          if [ $opr = "+" ] 
          then 
          	op=`expr $a + $b`
          	echo "$op"
          elif [ $opr = "-" ] 
          then 
          	op=`expr $a - $b`
          	echo "$op"
          elif [ $opr = "/" ] 
          then 
          	op=`expr $a / $b`
          	echo "$op"
          elif [ $opr = "X" ] 
          then 
          	op=`expr $a \* $b`
          	echo "$op"
          fi
          Raghu

          Comment

          • lifeisgreat20009
            New Member
            • Oct 2007
            • 70

            #6
            thank you very much..its working now

            Comment

            Working...