Find the answer to your Linux question:
Results 1 to 3 of 3
I have some project for linux class and we have to make a command line program to do something and I figured I'd make a simple calculator here is what ...
  1. #1
    Just Joined!
    Join Date
    May 2009
    Posts
    2

    Creating a simple Command line linux calculator

    I have some project for linux class and we have to make a command line program to do something and I figured I'd make a simple calculator here is what I have:


    I'm skipping most of the stupid code that isn't necessary aka most of the echos and user prompts

    but:
    The user is prompted to enter two numbers NUM1 and NUM2, after which they have another prompt to select which operator to use.

    Code:
    echo -n "please enter the first number"
    read NUM1
    
    echo -n "Please enter the second number."
    read NUM2s
    
    echo -n "Please enter which operator you would like to use"
    read selection
    
    case $selection in
    m) let ans=num1*num2 ; echo $ans;;
    *) echo -n "Please enter proper data"
    esac
    
    echo " "
    the output gives me a '0' every time..why is that?

  2. #2
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    That's probably because NUM1 and num1 are two different variable names. Case matters.

  3. #3
    Just Joined!
    Join Date
    May 2009
    Posts
    2
    Quote Originally Posted by i92guboj View Post
    That's probably because NUM1 and num1 are two different variable names. Case matters.
    Yeah, I shortly noticed that after I posted, forgot to edit. Here is my error:
    NewScript: line 26: let: =: syntax error: operand expected (error token 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
  •  
...