![`정수 표현식이 필요합니다`](https://linux55.com/image/130794/%60%EC%A0%95%EC%88%98%20%ED%91%9C%ED%98%84%EC%8B%9D%EC%9D%B4%20%ED%95%84%EC%9A%94%ED%95%A9%EB%8B%88%EB%8B%A4%60.png)
integer expression expected
다음 코드에서 오류가 발생합니다 .
#! /bin/bash
# test integer: evaluate the value of integer
int=-5
if [ -z "$int" ]; then
echo 'int is empty.' >&2
fi
if [ "$int" -eq 0 ]; then
echo "int is zero"
else
if [ '$int' -lt 0 ]; then
echo "int is negative."
else
echo 'int is positive.'
fi
if [ $((int % 2)) -eq 0 ]; then
echo 'int is even.'
else
echo 'int is odd.'
fi
fi
실행하고 오류 보고서 받기
$ bash test_integer.sh
test_integer.sh: line 14: [: $int: integer expression expected
int is positive.
int is odd.
여러 번 확인했지만 오류를 찾을 수 없었습니다.
책에 나와 있는 내용과 일치하는 것 같습니다.
내 코드에 무슨 문제가 있나요?
답변1
'$int'
는 인용 오류입니다. 스크립트를 실행하면 쉽게 확인할 수 있습니다.
bash -vx schript.sh
작은따옴표 안에는 확장이 없습니다. 다음을 수행해야 합니다.
if [ "$int" -lt 0 ]