[英]Bash Escaping @ Symbol In Variable
I'm having an issue with an if statement in a Bash script. 我在Bash脚本中的if语句有问题。 Here's the code:
这是代码:
if [[ "$AppleID" -ne "" ]]; then
echo "<result>${AppleID}</result>"
else
echo "No user logged in."
fi
Assuming $AppleID
is a string with a value of "test@email.com", the error message is as follows: 假设
$AppleID
是一个值为“ test@email.com”的字符串,错误消息如下:
[[: test@email.com: syntax error: invalid arithmetic operator (error token is "@email.com")
I've tried using sed to escape the characters, like this: 我试过使用sed来转义字符,如下所示:
if [[ `echo $(printf '%q' $AppleID) | sed -e 's/[\/&]/\\&/g'` -ne "" ]]; then
But I get the same error. 但是我得到了同样的错误。 How do I escape the @ symbol?
如何转义@符号?
-ne
is for comparing integers. -ne
用于比较整数。 You want !=
, or better yet, just use -n
(which tests if its argument is a non-empty string): 您想要
!=
,或者更好,只需使用-n
(它会测试其参数是否为非空字符串):
if [[ -n "$AppleID" ]]; then
echo "<result>${AppleID}</result>"
else
echo "No user logged in."
fi
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.