#!/bin/bash
for ((i=1 ;i<=3;i++))
do
echo "Enter gallon used(gal):"
read gal
echo "Enter Miles Obtained(mil):"
read mil
mileage=`echo $mil / $gal |bc`
echo "scale=4; $mileage " | bc
c=`echo $c + $mileage | bc`
echo "$c + $mileage = $c"
echo
done
Asked
Active
Viewed 196 times
-1

Chris Davies
- 116,213
- 16
- 160
- 287
2 Answers
1
Is c
your accumulator? Set it to zero to start with, then you will not get a syntax error in line 10.
You get an integer result because there is no operation in line 9. Merge lines 8 and 9 to
mileage=$(echo "scale=4; $mil / $gal" | bc)
and then mileage
will have a decimal result.
You do not do anything useful with $c
and fail to print it after the loop.

X Tian
- 10,463
-
Thanks that was helpful, now i have decimal result. I need to work on accumulating. – okuru joe Jun 29 '15 at 14:24
-
1
#!/usr/bin/env bash
# Above, get the path to BASH from the environment.
# Below, you could just set the total mileage here.
total_mileage=0
# Below, start from zero and count up for the three loops.
for ((i=0; i<3; i++)); do
# Below, use `-n` to prevent the new line.
# It's ok to use descriptive variable names.
# echo -n "Enter gallons used: "
# Below, quote variables.
# read "gallons"
# Using the suggestion for `read` from @roaima :
read -p "Enter gallons used : " "gallons"
# Use a regular expression (regex). Here, a number with optional decimal:
while [[ ! $gallons =~ ^[+-]?[0-9]+\.?[0-9]*$ ]]; do
echo "Please enter a number or [CTRL]+[C] to exit."
read -p "Enter gallons used : " "gallons"
done
# echo -n "Enter miles obtained: "
# read "miles"
# Using the suggestion for `read` from @roaima :
read -p "Enter miles obtained: " "miles"
while [[ ! $miles =~ ^[+-]?[0-9]+\.?[0-9]*$ ]]; do
echo "Please enter a number or [CTRL]+[C] to exit."
read -p "Enter miles obtained: " "miles"
done
# Below, backticks are antiquated.
mileage=$(echo "scale=4; ($miles) / ($gallons)" | bc)
echo "Mileage: $mileage"
total_mileage=$(echo "scale=4; $total_mileage + $mileage" | bc)
done
average_mileage=$(echo "scale=4; ($total_mileage) / ($i)" | bc)
echo "Average mileage is $average_mileage"
See these, too:

Christopher
- 15,911
-
I did this### #!/bin/bash
c=0 for ((i=1 ;i<=3;i++)) do echo "Enter gallon used(gal):" read gal echo "Enter Miles Obtained(mil):" read mil mileage=
– okuru joe Jun 29 '15 at 15:15echo "scale=6; $mil / $gal" |bc
echo "The miles/gallon of a tankful was: $mileage" c=echo "scale=6; $c + $mileage" |bc
done echo average=echo "scale=6; $c / 3" |bc
echo "The overall Average miles/gallon attained was :$average" -
-
-
You're running bash. Consider
read -p "Enter miles obtained: " miles
, etc. – Chris Davies Jun 29 '15 at 15:46 -
thanks, that part is working well now. But I still having difficulty terminating the program when user input -1 – okuru joe Jun 29 '15 at 15:54
-
Yes, you should always quote all references to shell variables (e.g.,
"$gallons"
and"$miles"
) unless you have a good reason not to, and you’re sure you know what you’re doing. But you don’t need to quote their names when you’re setting them;read gallons
is perfectly fine and safe. … … … … … … P.S. Why do you divide by 1 inbc
? – G-Man Says 'Reinstate Monica' Oct 12 '16 at 19:14
c=\
echo $c + $mileage | bc``;echo $c + $mileage | bc
is (obviously) run before the shell assigns its output to$c
, so$c
is undeclared while runningecho $c + $mileage | bc
– kos Jun 29 '15 at 14:02