Arithmetic calculation on real numbers in Bourne Shell Script

I am begining to learn bourne shell and as a practice I have written a script which when given the purchase price and percentage of discount calculates the savings.

I somehow cannot figure out why my script fails to do arthimatic calculation on real numbers.

Could anyone look at the script and recommend where is the flaw.

Thanks in advance

#!/bin/sh
echo "Enter the Price of the item \$ \c"
read price
echo "What Percentage of discount is available \c"
read per
echo "Your Total Savings are \c"
sav=`expr \( $per / 100 | bc -l \) \* $price`
echo $sav

Either use "expr" or "bc" but not both.
Always do multiply before divide.

sav=`echo "(($price * $per) / 100)" | bc -l`

Sorry, but that didn't work

What does your current script look like?
What happened when you ran it?
What answers did you give to the questions?

Complete modified script. Only calculation line has changed. I have just tried it in Bourne Shell, Korn Shell and Posix Shell.

#!/bin/sh
echo "Enter the Price of the item \$ \c"
read price
echo "What Percentage of discount is available \c"
read per
echo "Your Total Savings are \c"
sav=`echo "(($price * $per) / 100)" | bc -l`
echo $sav
1 Like

It works when I copied the entire script, may be I had left someting.
Great Thanks

And the following method works too

echo "scale=4; $per * $price / 100" | bc -l

or

sav=`echo "scale=4; $per * $price / 100" | bc -l`

Sometimes this format is easier to read.
The scale sets for # of decimal places,
then the math equation,
piped to the math calculator.

1 Like