在Bash if条件中使用(…)时出错

在Bash if条件中使用(…)时出错,bash,shell,syntax,Bash,Shell,Syntax,我在这一端有一个错误。/scriptWeb.sh:第22行:(:“test1”=“test1”:语法错误:应为操作数(错误标记为“test1”=“test1”) 请提供一些帮助(())只能用于整数算术,不能用于字符串比较。对于字符串,请使用[[]]构造,方法如下: #!/bin/bash A='';B='';C='';D='' function returnFunctionWebService() { touch test.json curl http://loc

我在这一端有一个错误。/scriptWeb.sh:第22行:(:“test1”=“test1”:语法错误:应为操作数(错误标记为“test1”=“test1”) 请提供一些帮助

(())
只能用于整数算术,不能用于字符串比较。对于字符串,请使用
[[]]
构造,方法如下:

#!/bin/bash
A='';B='';C='';D=''
function returnFunctionWebService()
{
        touch test.json
        curl http://localhost:9099/BelattarProject/StudentMail.php -o test.json
        for line in `cat test.txt`;do
                echo $line
                A=$(echo $line | cut -d ',' -f1)
                echo $A
                B=$(echo $line | cut -d ',' -f2)
                echo $B
                C=$(echo $line | cut -d ',' -f3)
                echo $C
                D=$(echo $line | cut -d ',' -f4)
                echo $D
        done
}
returnFunctionWebService
echo $A $B $C $D
if [ -n $A ]; then
        if(( "$A" = "\"test1\"" )); then
                echo -e "c'est juste"
                exit
        else
                echo -e "c'est pas juste"
                exit
        fi
fi
您的脚本中有许多问题。请在中检查


另见:


@RavinderSingh13不,
=
确实做字符串比较:)@PesaThe,真的,你能帮我粘贴链接(我也会从中学习):),老实说,我不知道它。@RavinderSingh13如果你搜索“bash compare string”,会得到很多结果。不仅仅是“应该”;它只能用于算术
((…)
是算术复合命令。@OmarAmaoun如果您做了此更改,您不应该有相同的问题。但是,您的脚本还有许多其他问题需要解决。按照其他人的建议,使用
jq
解析json,使用codeforster的建议使用shellcheck.net发现常见的脚本错误。另外,请阅读的答案,并仔细阅读中的许多内容。使用
jq
解析json可以省去很多麻烦。除了使用错误的括号外,
if
和第一个打开的括号之间必须有空格。
if [[ "$A" = "\"test1\"" ]]; then