0

I am doing the following script to know if profit or loss from entered selling price and cost price as;

echo enter selling price
read sprice
echo enter costprice
read cprice

if [ $sprice -lt $cp ]
  then 
    echo Loss
else
  echo Profit
fi

All the time, it is returning Profit , with an error code, like;

:~/shell$ sh shellb.sh
enter selling price
10
enter costprice
20
shellb.sh: 6: [: -lt: argument expected
Profit

what might be the reason, and how can I fix this?

0

Change the $cp variable to $cprice which is what you read in the line that says read cprice.

echo enter selling price
read sprice
echo enter costprice
read cprice

if [ $sprice -lt $cprice ]
  then 
    echo Loss
else
    echo Profit
fi  

The script returns Profit even if $sprice has the same value as $cprice, so to be accurate add these lines to your script:

elif [ $sprice -eq $cprice ]  
  then   
    echo Break\ even

So what you end up with is:

echo enter selling price
read sprice
echo enter costprice
read cprice

if [ $sprice -lt $cprice ]
  then 
    echo Loss
elif [ $sprice -eq $cprice ]  
  then   
    echo Break\ even
else
    echo Profit
fi
| improve this answer | |
  • I know, I was waiting for the time cap, I am active in SO and I know how this community run :) – Alfred Oct 13 '14 at 5:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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