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?

1

1 Answer 1

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
1
  • I know, I was waiting for the time cap, I am active in SO and I know how this community run :)
    – Alfred
    Oct 13, 2014 at 5:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .