[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: bc



On Tue, 26 Feb 2002, David Lupo wrote:

=>On Wed, Feb 27, 2002 at 02:30:45AM +0530, bulbultyagi now-india net in wrote:
=>> 
=>> echo Enter a value
=>> read number
=>> 
=>> answer=(echo 0.245 \* $number)|bc
=>> echo $answer
=>> 
=>> but answer remains a null variable always
=>> How do I carry out calculations using non integer values then ?
=>
=>The calculation is working, but you have left out the back-ticks (`)
=>around the expression to capture its output into the shell variable.
=>Try
=>
=>answer=`(echo 0.245 \* $number)|bc`

answer=`echo "0.245 * $number" | bc`

or

answer=$(echo "0.245 * $number" | bc)

What you did was to put parens around the echo command which only caused 
it to run in a subshell. 

Also, you *might* want to get thre digits of precision out of your answer. 
If so,...

answer=$(echo "scale=3; 0.245 * $number" | bc)

-- 
-Time flies like the wind. Fruit flies like a banana. Stranger things have -
-happened but none stranger than this. Does your driver's license say Organ
-Donor?Black holes are where God divided by zero. Listen to me! We are all-
-individuals! What if this weren't a hypothetical question? steveo syslang net






[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]