为什么xargs的退出代码根据“-I”选项的存在而不同?

问题描述 投票:0回答:1

阅读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.

我原以为,无论是否使用123 if any invocation of the command exited with status 1-125,都应该适用-I

您能分享任何见解来解释这个难题吗?

grep find posix xargs exit-code
1个回答
0
投票

以下是使用包装脚本帮助显示调用次数的-I选项与xargs的效果的证据:

cat ./grep.sh
#/bin/bash

echo "I am being invoked at $(date +%Y%m%d_%H-%M-%S)"
grep $@

(正在调用的实际命令,在这种情况下grep并不重要)

现在使用包装器脚本执行与问题相同的命令:

❯ find .  -name "1.txt" -o -name "2.txt" | xargs -I {}  ./grep.sh -q "b" {}
I am being invoked at 20190410_09-46-29
I am being invoked at 20190410_09-46-30

❯ find .  -name "1.txt" -o -name "2.txt" | xargs ./grep.sh -q "b"
I am being invoked at 20190410_09-46-53

我刚刚发现了一个类似问题答案的评论,回答了这个问题(完全归功于https://superuser.com/users/49184/daniel-andersson的智慧):

https://superuser.com/questions/557203/xargs-i-behaviour#comment678705_557230

此外,不带引号的空格不会终止输入项;相反,分隔符是换行符。 - 这是理解行为的核心。如果没有-I,xargs只将输入视为单个字段,因为换行不是字段分隔符。使用-I,突然换行是一个字段分隔符,因此xargs看到三个字段(它迭代)。这是一个非常微妙的观点,但在引用的手册页中进行了解释。

-I replace-str
   Replace occurrences of replace-str in the initial-arguments
   with names read from standard input.  Also, unquoted blanks do
   not terminate input items; instead the separator is the
   newline character.  Implies -x and -L 1.

基于此,

find .  -name "1.txt" -o -name "2.txt"  
#returns
# ./1.txt 
# ./2.txt

xargs -I {}  grep -q "b"  {}
# interprets the above as two separate lines since,
# with -I option the newline is now a *field separator*. 
# So this results in TWO invocations of grep and since one of them fails, 
# the overall output is 123 as documented in the EXIT_STATUS section

xargs grep -q "b"
# interprets the above as a single input field,
# so a single grep invocation which returns a successful exit code of 0 since the pattern was found in one of the files.
© www.soinside.com 2019 - 2024. All rights reserved.