如何比较包含双引号的变量

时间:2015-11-09 18:44:10

标签: bash shell escaping string-comparison

我在尝试:

<?php if (strpos($_SERVER['REQUEST_URI'],'example-match') !== false) 
{
    echo "<script> var phone_number = '555-555-5555';</script>";
} 
else 
{
    echo "<script> var phone_number = '1-800-555-5555';</script>";
}
?>

然而我得到的是一个错误,因为&#34;在$ FOO。 我怎么能逃脱&#34;在一个变量?或者强制bash真正比较变量?

1 个答案:

答案 0 :(得分:3)

您没有在bash下运行脚本。 Bash会接受非标准语法。其他炮弹可能会拒绝。例如,比较bash和dash:

$ bash script.sh 
$ dash script.sh 
script.sh: 4: [: Hi" a/b/c:: unexpected operator

如此处所示,您的脚本在bash下工作但不是破折号。问题是意外操作员。这是指==非POSIX。解决方案是(a)在bash下运行,或者(b)替换:

if [ "$FOO" == "$BAR" ] ; then

使用:

if [ "$FOO" = "$BAR" ] ; then

[...]内,符号=是用于字符串相等的标准符号。

相关问题