0

I can't seem to debug the error in the computation of

case $code in
    ${menuCode[0]} )
        CBcost=$(echo "${menuItemsCost[0]} * $quantity" |bc) 
        sumOfCheeseBurgers=$(echo "$sumOfCheeseBurgers + $CBcost" |bc) 
    ;;

I need to be able to add all the sum of Burgers and store it to sumOfBurgers everytime the user enters Y for another transaction. However, I keep getting a (standard_in) 1: syntax error. How do I properly express sumOfBurgers=sumOfBurgers + CBcost?

declare -a menuItems=( 'Cheese Burger' 'Ham Burger' 'Spaghetti' 'Fried  Chicken' 'Softdrinks' )
declare -a menuItemsCost=( 25.00 20.00 24.00 30.00 15.00 )
declare -a menuCode=( CB HB SP FC SD )

clear

printf "MENU\n"
printf "=================================\n"
printf "CODE\tDESCRIPTION\t PRICE\t|\n"
echo   "================================="
printf "${menuCode[0]}  |\t${menuItems[0]}\t| ${menuItemsCost[0]} |\n"
printf "${menuCode[1]}  |\t${menuItems[1]}\t| ${menuItemsCost[1]} |\n"
printf "${menuCode[2]}  |\t${menuItems[2]}\t| ${menuItemsCost[2]} |\n"
printf "${menuCode[3]}  |\t${menuItems[3]}\t| ${menuItemsCost[3]} |\n"
printf "${menuCode[4]}  |\t${menuItems[4]}\t| ${menuItemsCost[4]} |\n"
printf "=================================\n"

choice="Y"
while [ $choice == "Y" ] 
    do
    printf "What's your order please? \n[Enter Code]: "
    read code
        while [ "$code" != "CB" ] && [ "$code" != "HB" ] && [ "$code" != "SP" ] && 
                [ "$code" != "FC" ] && [ "$code" != "SD" ]   
            do 
                printf "Please enter a valid ORDER code. \n[Enter Code]: " 
                    read code
                    code=$code 
        done

printf "Enter Quantity: "
    read quantity
printf "Do another transaction [Y/N]?: "
    read choice

case $code in
     ${menuCode[0]} )
        CBcost=$(echo "${menuItemsCost[0]} * $quantity" |bc) 
        sumOfCheeseBurgers=$(echo "$sumOfCheeseBurgers + $CBcost" |bc) 
    ;;
esac

done

printf "$sumOfCheeseBurgers\n"
1
  • Btw, for correct useage of printf see: help printf. Commented Aug 24, 2015 at 9:01

1 Answer 1

1

You need to initialise sumOfCheeseBurgers before you use it here, otherwise it is an empty string which causes the syntax error from bc:

sumOfCheeseBurgers=$(echo "$sumOfCheeseBurgers + $CBcost" |bc)

Add the following line before the while loop:

sumOfCheeseBurgers=0

Or alternatively, as pointed out in the comments (thanks), you can use this syntax to assign a default value:

sumOfCheeseBurgers=$(echo "${sumOfCheeseBurgers-0} + $CBcost" |bc) 

If you run your script like bash -x script.sh, you can see this error more clearly:

++ echo ' + 50.00'
++ bc

As you can see, the string being passed to bc is missing a value before the +.

As an aside, you should be using format specifiers with printf. For example:

printf "${menuCode[0]}  |\t${menuItems[0]}\t| ${menuItemsCost[0]} |\n"

should be something like this:

printf '%s  |\t%s\t| %s |\n' "${menuCode[0]}" "${menuItems[0]}" "${menuItemsCost[0]}"

This is the correct way to use printf as it prevents characters within your variables from being interpreted as escape characters (for example, newlines or tab characters).

Sign up to request clarification or add additional context in comments.

2 Comments

... or use ${SumOfCheeseBurgers-0} in the echo.
Thanks, Tom. I appreciate the tips and the solution you provided. I've finished the program last night and got everything working fine. Some of the syntax in Shell is a bit different. I'll take note of the things I missed. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.