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

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}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.