bash 在脚本中检查差异退出状态
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23497492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Checking diff exit status in a script
提问by gnometorule
On the command line, after using diff on two files that differ, the command
在命令行上,在两个不同的文件上使用 diff 后,命令
echo $?
reports back '1'. When I try the same in a script, as follows:
报告回“1”。当我在脚本中尝试相同时,如下所示:
echo "` diff $F1 $F2`"
rv=$?
if [[ $rv == 1 ]]
then
echo "failed"
fi
then I never print 'failed' (even for differing files). Note that this is the bash shell, so the grammar should be fine (eg, if I check for '0' instead, it always prints).
然后我从不打印“失败”(即使对于不同的文件)。请注意,这是 bash shell,因此语法应该没问题(例如,如果我改为检查 '0',它总是会打印)。
How can I check if the diff command discovered differences, and process conditionally on that?
如何检查 diff 命令是否发现了差异,并有条件地进行处理?
This is under Ubuntu 12.04.
这是在 Ubuntu 12.04 下。
回答by nobody
You're not seeing the return value from diff
because the last command run is actually echo
and you're seeing its return value. You should be able to achieve the desired effect with the following code (capturing and then echoing the output of diff
is unnecessary - just let it write to stdout):
您没有看到返回值,diff
因为最后运行的命令实际上是echo
并且您看到了它的返回值。您应该能够使用以下代码实现所需的效果(捕获然后回显的输出diff
是不必要的 - 只需让它写入标准输出):
diff $F1 $F2
rv=$?
if [[ $rv == 1 ]]
then
echo "failed"
fi
Also, note that diff
returns a value greater than one on error (0
indicates identical files, 1
indicates different files). You may want to check for and handle that case.
另请注意,diff
错误时返回大于 1 的值(0
表示相同的文件,1
表示不同的文件)。您可能需要检查并处理这种情况。
回答by RedX
From your comment:
从你的评论:
But I would like to print the differences first, but also keep track of how many comparisons failed.
但我想先打印差异,还要跟踪有多少比较失败。
I don't know if diff outputs the number of differences in the exit code. I think not. But you could count the lines maybe...
我不知道 diff 是否输出退出代码中的差异数。我想不是。但你可以数数行数也许...
Here is how you can store the exit code and count the number of different lines
以下是存储退出代码和计算不同行数的方法
var=$(diff "$F1" "$F2")
#store diff exit code
exit_code=$?
# remember that this is not the same as count of differences
lines_output_by_diff=$(wc -l <<< "$var")
echo "$var"
if (($exit_code == 0)); then
echo "same"
else
echo "not same"
fi
回答by player3
It seems to be because, in your script, $?
is the return status of your echo
line (not the previous program), and echo will probably always work and return 0
.
这似乎是因为,在您的脚本中,$?
是您的echo
行(不是前一个程序)的返回状态,并且 echo 可能始终有效并返回0
。
回答by Red Cricket
You probably want to do this instead.
你可能想要这样做。
echo "`diff $F1 $F2`"
diff $F1 $F2 > /dev/null 2>&1
rv=$?
...
because $?
is get set to 0 by the successful execution of echo
.
因为$?
通过成功执行 被设置为 0 echo
。
And if you don't want to run diff
twice you could do this too ..
如果你不想跑diff
两次,你也可以这样做..
diff $F1 $F2 > /tmp/thediff 2>&1
if [ $? != 0 ]
then
cat /tmp/thediff
fi