【发布时间】:2019-04-10 17:25:04
【问题描述】:
阅读 xargs man page 后,我无法理解以下 xargs 调用的退出代码的区别。
(当我遇到这种行为时,最初的目的是结合 find 和 grep 来检查所有给定文件中是否存在表达式)
复制:
(如果使用 zsh 强制创建文件,则使用 >>!)
# Create the input files.
echo "a" >> 1.txt
echo "ab" >> 2.txt
# The end goal is to check for a pattern (in this case simply 'b') inside
# ALL the files returned by a find search.
find . -name "1.txt" -o -name "2.txt" | xargs -I {} grep -q "b" {}
echo $?
123 # Works as expected since 'b' is not present in 1.txt
find . -name "1.txt" -o -name "2.txt" | xargs grep -q "b"
echo $?
0 # Am more puzzled by why the behaviour is inconsistent
手册页上的 EXIT_STATUS 部分说:
xargs exits with the following status:
0 if it succeeds
123 if any invocation of the command exited with status 1-125
124 if the command exited with status 255
125 if the command is killed by a signal
126 if the command cannot be run
127 if the command is not found
1 if some other error occurred.
我会认为,无论是否使用-I,123 if any invocation of the command exited with status 1-125 都应该适用?
您能否分享任何见解来解释这个难题?
【问题讨论】:
-
我相信我已经在另一个问题的评论中找到了答案:superuser.com/questions/557203/…。详细信息作为答案发布
标签: grep find posix xargs exit-code