Bash字符串(命令输出)相等性测试 [英] Bash string (command output) equality test
本文介绍了Bash字符串(命令输出)相等性测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个简单的脚本来检查网页是否包含指定的字符串.看起来像:
I have a simple script to check whether webpage contains a specified string. It looks like:
#!/bin/bash
res=`curl -s "http://www.google.com" | grep "foo bar foo bar" | wc -l`
if [[ $res == "0" ]]; then
echo "OK"
else
echo "Wrong"
fi
如您所见,我希望获得确定",但得到错误".
As you can see, I am looking to get "OK", but got a "Wrong".
怎么了?
如果我使用if [$ res =="0"],它可以工作.如果我只使用res ="0"而不是res = curl...
,它也可以获得所需的结果.
If I use if [ $res == "0" ], it works. If I just use res="0" instead of res=curl...
, it also can obtain the desired results.
为什么会有这些差异?
推荐答案
我在 glenn jackman 中找到了答案的帮助.
I found the answer in glenn jackman's help.
在这个问题上我得到以下几点:
I get the following points in this question:
-
wc -l
的输出包含空格. - 使用
echo "$var"
而不是echo $var
进行调试
-
[[
保留var中所有字符的文字值. -
[
在执行之前将var扩展为其值,这是因为[实际上是test
cmd,因此它遵循
wc -l
's output contains whitespaces.- Debugging with
echo "$var"
instead ofecho $var
[[
preserves the literal value of all characters within the var.[
expands var to their values before perform, it's because [ is actually thetest
cmd, so it follows Shell Expansions rules.
这篇关于Bash字符串(命令输出)相等性测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文