0

hey there...could I please ask for some help for my programming assignment. I having a problem in assigning values to a variables inside a function. I don't know what im doing wrong...any suggestions could be a great help..here is my code

a = 0

myfunc(){

echo $1 "Computation"
a = `expr 3.14\* $2\*$2|bc`

echo a


}

myfunc $1 $2

What I would like to do is to enter a command line argument. the first is a string and the second is a integer. the function should compute for the are of a circle but whenever I echo the value of a a error occurs..could I please ask for some clarifications on what Im doing wrong...thanks

If my arguments are Circle 2
The proper output of the program should be:


Circle Computation
12.56

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by sknake
0

expr doesn't handle decimals/floating numbers and your assignment syntax is wrong. Use var=value without the spaces. This fixes everything but expr . I don't know your project requirements but you will need to find another way to handle the math. Bash can handle math on its own by the way:

#!/bin/bash
a=0

myfunc(){

echo "Computation"
#a=`expr 3.14\* ${2}\*${2}|bc`
a=`echo 5`
echo ${a}
}

myfunc ${1} ${2}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.