为什么基于“ -I”选项,xargs的退出代码会有所不同?

时间:2019-04-10 08:30:59

标签: grep find posix xargs exit-code

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

您能分享一些见解来解释这个难题吗?

1 个答案:

答案 0 :(得分:0)

在包装脚本的帮助下,这里显示了{args} -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.
相关问题