else 조건이 오류를 반환하는 경우 Ubuntu bash [: -lt: 인수가 필요함

else 조건이 오류를 반환하는 경우 Ubuntu bash [: -lt: 인수가 필요함

입력된 판매가와 원가에 따른 손익 여부를 알아보기 위해 다음 스크립트를 작성하고 있습니다.

echo enter selling price
read sprice
echo enter costprice
read cprice

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

항상 Profit다음과 같은 오류 코드와 함께 을(를) 반환합니다.

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

이유는 무엇일까요? 이 문제를 어떻게 해결할 수 있나요?

답변1

라고 표시된 줄에서 읽은 $cp변수를 변경하십시오 .$cpriceread cprice

echo enter selling price
read sprice
echo enter costprice
read cprice

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

스크립트는 $sprice와 동일한 값을 갖는 경우에도 Profit을 반환하므로 $cprice정확하려면 스크립트에 다음 행을 추가하십시오.

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

그래서 결국은 다음과 같습니다.

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

관련 정보