-1
#!/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
Chris Davies
  • 116,213
  • 16
  • 160
  • 287
  • And what's the reported error? – kos Jun 29 '15 at 13:53
  • For one you are trying to use a variable in it's own declaration and then echoing that the variable + another equals itself. – 123 Jun 29 '15 at 13:56
  • Yes, the problem is in this line: 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 running echo $c + $mileage | bc – kos Jun 29 '15 at 14:02
  • I am trying to calculate the mileage for 3 input and get the average. plus the mileage only come out as integer. I need decimals. – okuru joe Jun 29 '15 at 14:06
  • 1
    By the way, your conceptual algorithm is flawed — the average of a series of mileage values is generally not the average mileage. For example, if you use 4 gallons driving 80 miles up a mountain (→ 80/4 = 20 mpg) and then you use 1 gallon while driving/coasting 80 miles back down the mountain (→ 80/1 = 80 mpg), your average mileage is not (20+80)/2=50, it is (80+80)/(4+1)=160/5=32. – G-Man Says 'Reinstate Monica' Oct 12 '16 at 19:10

2 Answers2

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
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=echo "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"

    – okuru joe Jun 29 '15 at 15:15
  • What about when i want to exit the program when the user input -1? – okuru joe Jun 29 '15 at 15:19
  • And thanks, i like the way you comment on the program. – okuru joe Jun 29 '15 at 15:19
  • 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 in bc? – G-Man Says 'Reinstate Monica' Oct 12 '16 at 19:14