1

MY CODE :

echo "Please Enter The Purchase Price : " read $PurPrice echo "Please Enter The Selling Price : " read $SellPrice if [ "$PurPrice" -eq "$SellPrice" ] then echo "No Profit No Loss !" elif [ "$PurPrice" -gt "$SellPrice" ] then loss=echo "$pp - $sp" | bc -l echo "There is loss of $loss." else profit=echo "$sp - $pp" | bc -l echo "There is Profit $profit. " fi 

THE ERRORS :

Please Enter The Purchase Price : ': not a valid identifier ` Please Enter The Selling Price : ': not a valid identifier ` Newfile.sh: line 5: $'\r': command not found Newfile.sh: line 10: syntax error near unexpected token `elif' 'ewfile.sh: line 10: elif [ "$PurPrice" -gt "$SellPrice" ] 
5
  • 1
    Possible duplicate of Why is a shell script giving syntax errors when the same code works elsewhere? Commented Dec 30, 2015 at 21:45
  • Possible duplicate of (or at least, the same problem as) Assign curl output to variable in bash Commented Dec 30, 2015 at 21:52
  • 2
    Also not sure what this has to do with the vi editor... Commented Dec 30, 2015 at 21:53
  • DOS line-endings are a problem but so is using $ in the read commands. Commented Dec 30, 2015 at 21:55
  • On both of your "read" lines, you do not need the "$" symbol. "$" is used when you want the value of a variable. Commented Dec 30, 2015 at 21:56

1 Answer 1

2

Your code has lots of issues.

  • You were not consistent in your variable names (e.g. pp vs. PurPrice)
  • You only use $ when you want the value of a variable (not on read's for example).
  • You can't use strings with the integer test operators (-eq and so on).
  • You need backticks to get the output of commands you run.

Here is functional code:

#!/bin/sh echo "Please Enter The Purchase Price : " read PurPrice echo "Please Enter The Selling Price : " read SellPrice if [ $PurPrice -eq $SellPrice ] ; then echo "No Profit No Loss !" elif [ $PurPrice -gt $SellPrice ] ; then loss=`echo "$PurPrice - $SellPrice" | bc -l` echo "There is loss of $loss." else profit=`echo "$SellPrice - $PurPrice" | bc -l` echo "There is Profit $profit. " fi 

but note that the test operators (-eq -gt etc) only work on integers (entering a price like 1.99 will cause it to fail)!

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

3 Comments

Here's a fun solution to the "only works on integers" problem: stackoverflow.com/a/33759747/1072112
Quoting the variables in test/[ contexts is important.
How to get the floating Value ??

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.