Find the answer to your Linux question:
Results 1 to 5 of 5
Hey guys I'm new but trying to figure something out, I'm teaching myself the basic's of Shell Scripting, and decided a Calculator of sorts is as good a place as ...
  1. #1
    Just Joined!
    Join Date
    Apr 2011
    Posts
    3

    Noob Shell Scripting

    Hey guys I'm new but trying to figure something out, I'm teaching myself the basic's of Shell Scripting, and decided a Calculator of sorts is as good a place as any to start, However what I want to do is after each case statement to return you to the Original Question "Select Add...." is there a command to do this?

    I would guess its a form of flagging the start point and referencing it in each case. Heres My code so far

    Code:
    echo "Select Add (+), Sub (-), Multi(x), Divide(/)"
    read Opr
    
    echo "First Number"
    read Num1
    
    echo "2nd Number"
    read Num2
    
    case $Opr in
    +)
                    Sum=`expr $Num1 + $Num2`
                    echo "Answer = $Sum"
            ;;
    
    -)
                    Sum=`expr $Num1 - $Num2`
                    echo "Answer = $Sum"
            ;;
    
    /)
                    Sum=`expr $Num1 / $Num2`
                    echo "Answer = $Sum"
            ;;
    x|X)
                    Sum=`expr $Num1 \* $Num2`
                    echo "Answer = $Sum"
            ;;
    *)
                    echo "Error, try again - Press anything to continue"
            ;;
    
    esac

  2. #2
    Just Joined! hunter_thom's Avatar
    Join Date
    Apr 2010
    Posts
    89
    Wrap the whole thing in a while loop, and break the loop if the user wants to exit.

    something like:

    Code:
    while [ 1 ]; do
      echo "Continue?"
      read input
      if [ $input = "no" ]; then
        break
      fi
      # the rest of your calculator code
    done

  3. #3
    Just Joined!
    Join Date
    Apr 2011
    Posts
    3
    Quote Originally Posted by hunter_thom View Post
    Wrap the whole thing in a while loop, and break the loop if the user wants to exit.

    something like:

    Code:
    while [ 1 ]; do
      echo "Continue?"
      read input
      if [ $input = "no" ]; then
        break
      fi
      # the rest of your calculator code
    done
    Excellent Mate, I should be able to get it polished of from here.

  4. #4
    Just Joined!
    Join Date
    Apr 2011
    Posts
    3
    Why is the goto command not considered proper coding? surely if it works its grand?

  5. #5
    Trusted Penguin elija's Avatar
    Join Date
    Jul 2004
    Location
    Either at home or at work or down the pub
    Posts
    2,298
    goto is incredibly easy to abuse leading to unreadable and unmaintainable code which is a bad thing. Write your code as if the guy maintaining it after you it is a psycho with an axe and your address.

    More info
    If we hit that bullseye, the rest of the dominoes will fall like a house of cards. Checkmate! (Zapp Brannigan)


    My new blog. It's probably not as good as I think it is.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...