
그래서 왜 이런 일을하는지 잘 모르겠습니다. 내 if 문은 두 번째 변수를 변수 대신 리터럴 문자열로 계속 해석합니다. 아래는 내 정확한 코드입니다.
lights() {
bulb1state=$(gatttool -b D8:6F:4B:09:AC:E6 --char-read -a 0x001b)
echo $bulb1state
bulb2state=$(gatttool -b DA:5A:4B:09:AC:E6 --char-read -a 0x001b)
bulb3state=$(gatttool -b AC:E6:4B:07:39:E9 --char-read -a 0x0018)
bulb4state=$(gatttool -b AC:E6:4B:08:40:50 --char-read -a 0x0018)
offstate="Characteristic value/descriptor: 00 00 00 00"
echo $bulb1state
echo $offstate
if [ "$offstate" = "$bulb1state" ]; then
echo $bulb1state
echo "bulb1 state = off"
gatttool -b D8:6F:4B:09:AC:E6 --char-write -a 0x001b -n ff000000
gatttool -b DA:5A:4B:09:AC:E6 --char-write -a 0x001b -n ff000000
gatttool -b AC:E6:4B:07:39:E9 --char-write -a 0x0018 -n ff000000
gatttool -b AC:E6:4B:08:40:50 --char-write -a 0x0018 -n ff000000
fi
}
내 결과:
>lights
Characteristic value/descriptor: 00 00 00 00
Characteristic value/descriptor: 00 00 00 00
Characteristic value/descriptor: 00 00 00 00
마지막 2개의 echo 문이 표시되지 않는 이유를 이해할 수 없습니다.
편집:bulb1state에 공백이 있었습니다. 이것은 해결 방법을 찾기에 충분하지만 "-eq" 연산자가 아닌 "=" 연산자를 사용할 때 if 문의 두 번째 용어가 리터럴 문자열로 해석되는 이유가 여전히 궁금합니다. . 또한 어떤 변수가 첫 번째인지 두 번째인지는 중요하지 않았습니다.