本文介绍了字符串之间的Bash比较-相等但不相等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想在Bash中的2个字符串之间做一个非常简单的比较:

I just wanted to do a really simple comparison between 2 strings in Bash :

stat=`curl -Is $url | head -n 1`
echo $stat
if [ "$stat" = "HTTP/1.1 200 OK" ];then
    echo "$symbol is OK"
    echo $stat
    valide=$(( $valide + 1 ))
else
    echo "$symbol is 404"
    echo $stat
    err=$(( $err + 1 ))
fi

但是即使"stat"完全相同,结果仍然是不相等的:

But even if "stat" is completely the same, the result stay that it does not equal :

HTTP/1.1 200 OK
AAA is 404
HTTP/1.1 200 OK

我如何修改我的代码?

在到达此代码示例之前,字符串中的"/"(意外的运算符",...)也存在很多错误,我尝试了许多不同的方法,例如包含("200" ),结果相同.

I had also a lot of errors with "/" ("unexpected operator", ...) in the string before arrive to this code sample, and i tried many different approaches like contains ("200") with the same result.

提前谢谢!

推荐答案

curl输出在行尾包含 CR ,您需要将其删除.

The curl output includes a CR at the end of the lines, you need to remove it.

stat=`curl -Is $url | head -n 1 | tr -d '\r'`

这篇关于字符串之间的Bash比较-相等但不相等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 13:06
查看更多