下面有一个简单的脚本
#!/bin/bash
RESPONSE=`jq '.errorCode' /data/logs/errorLog.log`
echo $RESPONSE
if [[ "$RESPONSE" = "INVALID_API_CREDENTIALS" ]]
then
echo "Registration unsuccessful..."
else
echo "Registration successful..."
fi
输出root@ubuntu-test:/data/logs# ./test.sh
"INVALID_API_CREDENTIALS"
Registration successful...
root@gubuntu-test:/data/logs#
为什么else块在这里执行?
从Gordon Davisson的评论中可以看出哪里出了问题。我想补充的是,jq -r ...
也可以解决这个问题。我建议使用bash regex=~
而不是=
,它可以节省一些空间:
if [[ $RESPONSE =~ INVALID ]]
then
echo "Registration unsuccessful..."
else
echo "Registration successful..."
fi
使用shopt -s nocasematch