About while do loop,what is wrong with this code ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • redcodeheaven
    New Member
    • Nov 2008
    • 13

    About while do loop,what is wrong with this code ?

    Hello my teacher gave me an exercise to enter my name and 2 numbers the output must show the sum and the product of these 2 numbers and keep repeating the same procedure using a while loop till i answered "no" to a question if i want to continue,so I wrote this shell code but something is wrong ,I am using Ubuntu and vi editor:
    Code:
    #! /bin/bash
    while :
    do
    echo "please enter your name"
    read name
    echo"please enter your first number"
    read firstnumber
    echo"please enter your second number"
    read secondnumber
    echo "sum of the 2 numbers are"
    expr $firstnumber + $secondnumber
    
    echo "product of the 2 numbers is"
    expr $firstnumber \*  $secondnumber
    
    echo " do you wish to continue? yes/no"
    read input
    if [$input =="no"]; then
    break
    fi
    done
    # anyway to improve it and what is wrong? thank you for help.
    Last edited by Nepomuk; Nov 10 '08, 02:57 PM. Reason: Please use [CODE] tags
  • ashitpro
    Recognized Expert Contributor
    • Aug 2007
    • 542

    #2
    Code:
    #! /bin/bash
    while :
    do
    echo "please enter your name"
    read name
    echo "please enter your first number"
    read firstnumber
    echo "please enter your second number"
    read secondnumber
    echo "sum of the 2 numbers are"
    expr $firstnumber + $secondnumber
    
    echo "product of the 2 numbers is"
    expr $firstnumber \* $secondnumber
    
    echo " do you wish to continue? yes/no"
    read input
    if [ $input == "no" ]
    then
    break
    fi
    done
    I've made some minor changes for you....
    Compare this with original code...you'll come to know about problems...

    Comment

    • redcodeheaven
      New Member
      • Nov 2008
      • 13

      #3
      Thank you ashitpro for your help but still the code is not working properly:
      first it says about :"command not found" and secondly it seems like i can't get out of the loop when i press no.
      Any further help ?thank you

      Comment

      • ashitpro
        Recognized Expert Contributor
        • Aug 2007
        • 542

        #4
        Originally posted by redcodeheaven
        Thank you ashitpro for your help but still the code is not working properly:
        first it says about :"command not found" and secondly it seems like i can't get out of the loop when i press no.
        Any further help ?thank you
        Have you check out the spaces, that I've added..?
        Have you compared this code with original one?
        Use if then exactly as shown

        if [ $input == "no" ]
        then

        there are spaces after and before $input and "no"

        Comment

        Working...