bash diff 的错误退出值是多少?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6971284/
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
What are the error exit values for diff?
提问by sid_com
On the diff
man-page I've found these exit values:
在diff
手册页上,我发现了这些退出值:
0 No differences were found.
1 Differences were found.
>1 An error occurred.
Are there different exit values above 1 for different errors?
对于不同的错误,是否有高于 1 的不同退出值?
回答by Frédéric Hamidi
It depends on your diff
command. Mine (GNU diffutils 3.0) says:
这取决于你的diff
命令。我的(GNU diffutils 3.0)说:
An exit status of
0
means no differences were found,1
means some differences were found, and2
means trouble. Normally, differing binary files count as trouble, but this can be altered by using the-a
or--text
option, or the-q
or--brief
option.
退出状态
0
表示没有发现差异,1
表示发现了一些差异,2
表示有问题。通常,不同的二进制文件算作麻烦,但这可以通过使用-a
or--text
选项或-q
or--brief
选项进行更改 。
回答by David W.
There maybe, or there may not be different error codes depending upon the version of diff you use. If I remember correctly, the standard BSD diff always returned an exit code of 0, 1, or 2.
根据您使用的 diff 版本,可能有或可能没有不同的错误代码。如果我没记错的话,标准的 BSD 差异总是返回 0、1 或 2 的退出代码。
However, the manpage isn't mapping out everything that diff might do, but the documentation you can use for using diff command. In a shell script, I want to know if the files matched (exit = 0) or didn't match (exit = 1). However, in my shell script, I also want to know that the diff command itself didn't work.
但是,联机帮助页并未列出 diff 可能执行的所有操作,而是列出了可用于使用 diff 命令的文档。在 shell 脚本中,我想知道文件是匹配 (exit = 0) 还是不匹配 (exit = 1)。但是,在我的 shell 脚本中,我还想知道 diff 命令本身不起作用。
diff $file1 file2 > /dev/null 2>&1
error=$?
if [ $error -eq 0 ]
then
echo "$file1 and $file2 are the same file"
elif [ $error -eq 1 ]
then
echo "$file1 and $file2 differ"
else
echo "There was something wrong with the diff command"
fi
Imagine if I was told that 2 meant the diff command failed, but a newer version of the diff command made a distinction between a file you can't read (exit = 2) and a missing file (exit = 3). Now, imagine if I did the following in an earlier version of the diff command, but $file2
didn't exist:
想象一下,如果有人告诉我 2 意味着 diff 命令失败,但较新版本的 diff 命令区分了无法读取的文件(exit = 2)和丢失的文件(exit = 3)。现在,想象一下,如果我在较早版本的 diff 命令中执行以下操作,但$file2
不存在:
diff $file1 file2 > /dev/null 2>&1
error=$?
if [ $error -eq 2 ]
then
echo "There was something wrong with the diff command"
elif [ $error -eq 1 ]
then
echo "$file1 and $file2 differ"
else
echo "$file1 and $file2 are the same file"
fi
In the above code, I checked for the error code of 2 and 1, but not 3. So, instead of detecting a missing file, I assume that the files match.
在上面的代码中,我检查了错误代码 2 和 1,但没有检查 3。因此,我假设文件匹配,而不是检测丢失的文件。
The manpage is trying to make sure that future upgrades to the OS don't cause most of your shell scripts to suddenly fail. It's why there was a separate awk
and nawk
command and a separate grep
and egrep
command.
该联机帮助页试图确保将来对操作系统的升级不会导致您的大多数 shell 脚本突然失败。这就是为什么有一个独立的awk
,并nawk
命令和一个单独的grep
和egrep
命令。
*Updated as per comment by @chus.
*根据@chus 的评论更新。
回答by Rossano Fenner
In my case diff returned 127, searched for it and find it in the tldp.org "Exit Codes With Special Meanings"
在我的情况下,diff 返回 127,搜索它并在 tldp.org“具有特殊含义的退出代码”中找到它
127 "command not found" illegal_command Possible problem with $PATH or a typo.
127 "command not found" invalid_command $PATH 可能有问题或拼写错误。
I used an incorrect path to diff. :)
我使用了不正确的差异路径。:)
Font: tldp.org/LDP/abs/html/exitcodes.html
字体:tldp.org/LDP/abs/html/exitcodes.html