bash: 忽略退出代码,但保留输出重定向
bash: ignore exit code, but retain output redirect
我有一个脚本,其最后一行是:
git diff --no-index -- ./a.json ./b.json > ./a-b.diff
diff returns 当文件不同时 1
的退出代码,这会导致我的脚本报告错误。
如何忽略 diff 的退出代码,但保留内容的输出重定向到 diff 文件?
尝试:
git diff --no-index -- ./a.json ./b.json > ./a-b.diff || true
或:
git diff --no-index -- ./a.json ./b.json > ./a-b.diff || :
或者,只要您不使用 bash -e
:
git diff --no-index -- ./a.json ./b.json > ./a-b.diff
true
基本上,您可以在 diff 之后使用您知道会产生零(成功)退出代码的任何命令。
注意:您必须在 git diff --no-index
之后使用 || true
的原因是您 不能 忽略它的退出代码。
Git 2.25(2020 年第一季度)使这一点变得更加清晰。
参见 commit 0115e5d (29 Oct 2019) by Denton Liu (Denton-L
)。
(由 Junio C Hamano -- gitster
-- in commit d4924ea 合并,2019 年 12 月 1 日)
git-diff.txt
: document return code of --no-index
Signed-off-by: Denton Liu
Within diff_no_index()
, we have the following:
revs->diffopt.flags.exit_with_status = 1;
...
/*
* The return code for --no-index imitates diff(1):
* 0 = no changes, 1 = changes, else error
*/
return diff_result_code(&revs->diffopt, 0);
Which means when git diff
is run in --no-index
mode, --exit-code
is implied.
However, the documentation for this is missing in git diff
.
Add a note about how --exit-code
is implied in the --no-index
documentation to cover this documentation blindspot.
我有一个脚本,其最后一行是:
git diff --no-index -- ./a.json ./b.json > ./a-b.diff
diff returns 当文件不同时 1
的退出代码,这会导致我的脚本报告错误。
如何忽略 diff 的退出代码,但保留内容的输出重定向到 diff 文件?
尝试:
git diff --no-index -- ./a.json ./b.json > ./a-b.diff || true
或:
git diff --no-index -- ./a.json ./b.json > ./a-b.diff || :
或者,只要您不使用 bash -e
:
git diff --no-index -- ./a.json ./b.json > ./a-b.diff
true
基本上,您可以在 diff 之后使用您知道会产生零(成功)退出代码的任何命令。
注意:您必须在 git diff --no-index
之后使用 || true
的原因是您 不能 忽略它的退出代码。
Git 2.25(2020 年第一季度)使这一点变得更加清晰。
参见 commit 0115e5d (29 Oct 2019) by Denton Liu (Denton-L
)。
(由 Junio C Hamano -- gitster
-- in commit d4924ea 合并,2019 年 12 月 1 日)
git-diff.txt
: document return code of--no-index
Signed-off-by: Denton Liu
Within
diff_no_index()
, we have the following:revs->diffopt.flags.exit_with_status = 1; ... /* * The return code for --no-index imitates diff(1): * 0 = no changes, 1 = changes, else error */ return diff_result_code(&revs->diffopt, 0);
Which means when
git diff
is run in--no-index
mode,--exit-code
is implied.
However, the documentation for this is missing ingit diff
.Add a note about how
--exit-code
is implied in the--no-index
documentation to cover this documentation blindspot.