Problem with calculator script


 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Problem with calculator script
# 1  
Old 12-09-2010
Problem with calculator script

I'm having some trouble implementing a basic calculator using command line options. The script is supposed to take (multiple) arguments -a,-d,-m,-s for addition, multiplication, division, and subtraction. I'm pretty sure I know how to parse through the options with getopt(), but I have no idea how to get both arguments to operate on (ex: 7 -a 7, no idea how to get both 7's). I'm pretty sure I can get the first one, but no idea how to get the second.

Here's my current code:

Code:
#!/bin/bash

function printhelp {
echo "mymath.sh usage:"
echo "   mymath.sh [-options] [num1 num2]"
echo ""
echo "where options include:"
echo ""
echo " options that require 2 arguments (multiple options are allowed):"
echo "   -a   add           add num1 and num2"
echo "   -s   subtract      perform num1 minus num2"
echo "   -d   divide        divide num1 by num2 (integer division)"
echo "   -m   multiply      multiply num1 and num2"
echo ""
echo " options that cannot take any arguments, nor any other option:"
echo "   -h   help          display this message"
echo "   -v   version       provides the script's version (hard-coded)"
}

function printversion {
echo "Welcome to MyMath! Current version is v1.0"
}

function add {
   sum="$1 + $2"
   echo $sum
}

function multiply {
   product="$1" * "$2"
   echo $product
}

function subtract {
   diff="$1" - "$2"
   echo $diff
}

function divide {
   quotient="$1"/"$2"
   echo $quotient
}

if [ $# -le 1 ];
then
   if [ "$1" == "-h" ]; then
      printhelp
   elif [ "$1" == "-v" ]; then
      printversion
   else 
      printhelp
   fi
fi

while getopts "a:s:d:m:" opt
do
  case "$opt" in
    a) add $oper1 $oper2    
    ;;
    s) subtract $oper1 $oper2
    ;;
    d) divide $oper1 $oper2
    ;;
    m) multiply $oper1 $oper2
    ;;
    \?) 
    echo "Invalid option: -$OPTARG" >&2
    echo ""
    printhelp;;
    :)
        echo "Option -$OPTARG requires an argument." >&2
    printhelp
        exit 1
      ;;

  esac
done

Thanks for any help.
# 2  
Old 12-09-2010
take both values in 1 argument separated by ;(comma) or something else. then split values to use it as individuals.

Code:
mymath.sh -a 12;24

and in your code,

val1=`echo $optarg | cut -d";" -f1`
val2=`echo $optarg | cut -d";" -f2`

now pass val1 and val2 to your applicable function.
R0H0N
# 3  
Old 12-09-2010
Thanks for the help, but that's not allowed.

Has to be in the form

mymath.sh [-options] [-args]

Valid examples:

mymath.sh -a 12 39
mymath.sh -m -d 44 17
mymath.sh 35 -a -d 88 -m
# 4  
Old 12-09-2010
Code:
function subtract {
   diff="$1" - "$2"
   echo $diff
}

So your just printing out the equation?

If you want to do the math use

Code:
function subtract {
   diff=`expr $1 - $2`
   echo $diff
}

# 5  
Old 12-09-2010
Yep, just figured that out. Not the part I'm having trouble with though. New code will do mymath -a 7 7 but not mymath 7 -d 7

fixed all of the functions, now use
Code:
  case "$opt" in
    a) oper1="$OPTARG"
    shift
    oper2=$OPTARG
    add $oper1 $oper2    
    ;;

for each case. Works with mymath -a 7 7, but not the other forms, and doesn't check to see if the user had more than two numbers (it can only take two)
# 6  
Old 12-09-2010
Quote:
Originally Posted by zkapopou
Thanks for the help, but that's not allowed.

Has to be in the form

mymath.sh [-options] [-args]

Valid examples:

mymath.sh -a 12 39
mymath.sh -m -d 44 17
mymath.sh 35 -a -d 88 -m
Ok... Then u have to add below mentioned check conditions in your script.

1.> Make sure that only 3 command line arguments are entered.

Code:
if [ $# -gt 3  ] ; then display usage

2.> Make sure that arguments are entered properly. I mean first argument must be option and other two must be numbers.

Code:
echo $1 | egrep "^[+-][asdmhv]$" | if [ $? -gt 0 ] ; then ; exit ; fi
echo $2 | egrep "^[+-]*[0-9]$" | if [ $? -gt 0 ] ; then ; exit ; fi
echo $3 | egrep "^[+-]*[0-9]$" | if [ $? -gt 0 ] ; then ; exit ; fi

If these conditions are not meet then display usage and simply exit.
R0H0N
# 7  
Old 12-09-2010
Quote:
Originally Posted by R0H0N
Ok... Then u have to add below mentioned check conditions in your script.

1.> Make sure that only 3 command line arguments are entered.

Code:
if [ $# -gt 3  ] ; then display usage

2.> Make sure that arguments are entered properly. I mean first argument must be option and other two must be numbers.

Code:
echo $1 | egrep "^[+-][asdmhv]$" | if [ $? -gt 0 ] ; then ; exit ; fi
echo $2 | egrep "^[+-]*[0-9]$" | if [ $? -gt 0 ] ; then ; exit ; fi
echo $3 | egrep "^[+-]*[0-9]$" | if [ $? -gt 0 ] ; then ; exit ; fi

If these conditions are not meet then display usage and simply exit.
Believe me, I would love to do just that; however, like I stated above, the program must be able to accept multiple options (so if the user typed mymath -a -d -m -s 7 7, the script would output the results of each operation).
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell Script Calculator

Hello, I have to make a calculator in shell script. But I get this error. Can someone help me please? c.sh: 3: c.sh: i: not found That's my code. ========================================================================== #Calculator i = "yes" while do echo What operation... (2 Replies)
Discussion started by: KJN
2 Replies

2. Shell Programming and Scripting

Pls help with script made a calculator

Hello, I'm in need with a little help for my script please this is the brief i need to complete which I haven't been able to do: On option 7 stop the calculator The calculator will keep running until option 7 is chosen. Any other option than 1-7 will generate an error message. Pls any help... (1 Reply)
Discussion started by: linuxepicuser
1 Replies

3. Shell Programming and Scripting

CSH Calculator Script

Using the C Shell, I'm building a script that will compute simple mathematical computations (addition, subtraction, multiplication, division). The user will enter two integers (operands) on the command line separated by the operation (operator) they wish to perform. Example of the command line... (7 Replies)
Discussion started by: ksmarine1980
7 Replies

4. Homework & Coursework Questions

Simple Calculator

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known/data: Script a simple calculator. In the command line enter the script file /home/etc/mycalc or /home/etc/mycalc 1 +... (6 Replies)
Discussion started by: herb bertz
6 Replies

5. Shell Programming and Scripting

chmod calculator script

so just spit ballin here, i was wondering if anybody knew how to make a chmod calculator script. basically go to this website http://mistupid.com/internet/chmod.htm i would like something like this that i can use in a terminal tho. so like i run the scrip and it ask for owner what... (1 Reply)
Discussion started by: hookitup
1 Replies

6. Shell Programming and Scripting

Help with calculator code

Hi Guys, I found this code in net.. it is working fine.. But can anybody explain me the sed statement used in the code.. echo "Enter the expression:\c" read express eval echo "$express"|sed 's/^/'$precision' \ /'|bc -l|\ sed -n '1,${ /syntax/!{ } ... (2 Replies)
Discussion started by: mac4rfree
2 Replies

7. UNIX for Dummies Questions & Answers

calculator

hi, im new to the unix system and scripting and was wondering if anyone could help me with this problem iv been havin... i want the system to: 1. ask me for a number 2. ask me for a command to use on that number (* + - /) 3. ask me for another number 4. then ask me for another command, if the... (2 Replies)
Discussion started by: jdougy
2 Replies

8. Shell Programming and Scripting

Calculator

I am pretty new to the Unix word, and have created a working calculator script. I have one problem. It doesn't use any decimals, it rounds off to the nearest whole number. 1 #!/bin/ksh 2 while true; do 3 echo -n "Enter the first integer: "; read IN1 4 test... (2 Replies)
Discussion started by: ironhead3fan
2 Replies

9. Shell Programming and Scripting

calculator program..

Hey can anyone tell me the korn script code to implement an interactive integer calculator using the shell's built in arithemetic expression evaluation (2 Replies)
Discussion started by: sahithi_khushi
2 Replies
Login or Register to Ask a Question