Find the answer to your Linux question:
Results 1 to 4 of 4
Hi I know that many forums might frown upon this but i need help figuring out a problem with my script for my homework i have to write a bash ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2012
    Posts
    2

    Help with writing a bash script


    Hi I know that many forums might frown upon this but i need help figuring out a problem with my script for my homework i have to write a bash script to make a simple POS application that will calculate tax and calculate payments i have written almost all of my lines but i am still getting errors here is my code
    Code:
    clear
    max=0
    counter=0
    total=0
    echo "please enter price"
    read number
    let min=$number
    let max=$number
    while test $number
    do
    let total=$total+$number
    let counter=$counter+1
    echo "please enter next price"
    read number
    if test $number -gt $max
    then
    let max=$number
    fi
    if test $min -gt $number
    then 
    let min=$number
    fi
    done
    if test 1000 -gt $total
    then
    total=$(echo $total*1.06 | bc)
    fi
    if test $total -gt 1000
    then
    total=$(echo $total*1.03 | bc)
    fi
    echo "your total is:" $total
    echo "please enter payment"
    read payment
    total=$(echo $total-$payment | bc)
    while test $(echo $total -gt 0 |bc)
    do 
    echo "total is still:" $total "please enter another payment"
    read payment
    let total=$(echo $total-$payment | bc )
    done
    echo "you purchased" $counter "items"
    echo "your most expensive item costs:" $max
    echo "your least expensive item costs:" $min
    please any help is greatly appreciated and also my teacher told my that i can google and do whatever i need to to figure it out because he couldn't even figure out what the problem was i just want to know what i did wrong in this script

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    yes, homework questions are against the forum rules.

    however, that does not prevent us from giving you a helpful nudge in the right direction. but you have not given us the right information. just post exactly the error you are seeing, and the command(s) preceding it, exactly as you typed them.

    and i seriously doubt your teacher could not tell what was wrong w/a simple Bash script!

  3. #3
    Just Joined!
    Join Date
    Aug 2012
    Posts
    2
    the first error i am getting is line 29: test: 39.22: integer expression expected but it is giving me the correct total

    the second error i am getting is (standard_in) 1: parse error

  4. #4
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    okay, the integer expression expected error is b/c Bash arithmetic can't handle floating point numbers (e.g. 39.22). here are a couple of ideas:

    - multiply all numbers by 100 and strip the zeros. then use the resulting value as your variable.

    Code:
    echo 39.22 \* 100|bc -l|cut -f1 -d.
    - use Perl (this is what I'd do)

    The (standard_in) errors are from the bc command. It is probably b/c one of your variables was not set.

    Note: you can set Bash debugging by using "set -x" at the top of your bash script.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •