Find the answer to your Linux question:
Results 1 to 4 of 4
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    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
    echo "please enter price"
    read number
    let min=$number
    let max=$number
    while test $number
    let total=$total+$number
    let counter=$counter+1
    echo "please enter next price"
    read number
    if test $number -gt $max
    let max=$number
    if test $min -gt $number
    let min=$number
    if test 1000 -gt $total
    total=$(echo $total*1.06 | bc)
    if test $total -gt 1000
    total=$(echo $total*1.03 | bc)
    echo "your total is:" $total
    echo "please enter payment"
    read payment
    total=$(echo $total-$payment | bc)
    while test $(echo $total -gt 0 |bc)
    echo "total is still:" $total "please enter another payment"
    read payment
    let total=$(echo $total-$payment | bc )
    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
    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
    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. $spacer_open
  5. #4
    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.

    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