简体   繁体   English

为什么由cygwin运行时shell中的字符串比较不起作用

[英]Why doesn't string comparison in shell work when run by cygwin

I have a very simple script to check the status code returned by a website. 我有一个非常简单的脚本来检查网站返回的状态码。 I have curl for windows installed from https://curl.haxx.se/download.html . 我为从https://curl.haxx.se/download.html安装的Windows安装了curl。 When I run this shell script using git bash it works. 当我使用git bash运行此shell脚本时,它可以工作。

checkServer.sh: checkServer.sh:

code=`curl -I -sL -w "%{http_code}\\n" "www.google.com" -o /dev/null`

echo "Found code $code"
echo "Found endUrl $endUrl"

echo "x$code"
echo "x200"

if [ "x$code" = "x200" ]; then
    echo "got a 200"
fi

echo "done"

output: 输出:

$ sh "C:\Scripts\checkServer.sh"
Found code 200
Found endUrl
x200
x200
done

Why isn't this working? 为什么这不起作用? Is there some hidden character in the string returned by curl specifically when it's run in cygwin? 在cygwin中运行时,curl返回的字符串中是否存在某些隐藏字符? I'm already doing a find replace on the script with \\r\\n --> \\n so I don't believe it's line endings but who knows. 我已经在脚本上用\\ r \\ n-> \\ n进行查找替换,所以我不认为这是行尾,但谁知道。

user1934428's comment showed me how to find the hidden character returned in curl's httpcode. user1934428的注释向我展示了如何查找curl的httpcode中返回的隐藏字符。

using printf %s "$code" | od -cx 使用printf %s "$code" | od -cx printf %s "$code" | od -cx in the script to output code with any hidden characters I could see: 脚本中的printf %s "$code" | od -cx输出带有隐藏字符的代码,我可以看到:

Found code 200
Found endUrl
x200
0000000   2   0   0  \r
           3032    0d30
0000004
x200
done

So there's a \\r at the end of it. 因此,在末尾有一个\\ r。 This came from the \\\\n in the curl command which I'm not even sure why I had in the first place. 这来自curl命令中的\\\\n ,我什至不确定为什么要放在第一位。 Removing that fixed it. 删除固定它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM