Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.
#!/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
share|improve this question

closed as unclear what you're asking by cuonglm, X Tian, Anthon, maxschlepzig, Jenny D Jun 29 at 16:27

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

    
And what's the reported error? –  kos Jun 29 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. –  User112638726 Jun 29 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 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 at 14:06

2 Answers 2

#!/usr/bin/env bash

# Above, get the path to BASH from the envvironment.
# 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)/1" | bc)
    echo "Mileage: $mileage"

    total_mileage=$(echo "scale=4; ($total_mileage + $mileage)" | bc)

done

average_mileage=$(echo "scale=4; ($total_mileage / ($i))/1" | bc)
echo "Average mileage is $average_mileage"

See these, too:

share|improve this answer
    
#### 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 at 15:15
    
What about when i want to exit the program when the user input -1? –  okuru joe Jun 29 at 15:19
    
And thanks, i like the way you comment on the program. –  okuru joe Jun 29 at 15:19
    
You're running bash. Consider read -p "Enter miles obtained: " miles, etc. –  roaima Jun 29 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 at 15:54

Is c your accumulator ? set it to zero to start with, then you will not get syntax error in line 10

you get an integer result because there is no operation in line 9, Merge lines 8 and 9 to then milage will have a decimal result.

mileage=`echo "scale=4; $mil / $gal" |bc`

You do not do anything useful with $c and fail to print it after the loop.

share|improve this answer
    
Thanks that was helpful, now i have decimal result. I need to work on accumulating. –  okuru joe Jun 29 at 14:24
    
And yes c is suppose to be my accumulator –  okuru joe Jun 29 at 14:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.