简体   繁体   English

查找:如何强制非零退出状态

[英]find: How to force a non-zero exit status

I have glossed over the man page for find and -quit seems to partly do what I want, except it will only cause find to return non-zero if an error has occurred.我已经掩盖了 find 的手册页,并且-quit似乎部分地做了我想要的,除了它只会在发生错误时导致find返回非零。 So how can find be forced to return non-zero or at least be spoofed into returning non-zero in a way that is readable to maintainers?那么如何强制find以维护者可读的方式返回非零值或至少被欺骗返回非零值呢? So far I have this example:到目前为止,我有这个例子:

$ find . -maxdepth 2 -type f \( -exec echo {} \; -o \( -printf "FAIL\n" -a -quit \) \)
./scooby
./shaggy
./velma
./daphne
./fred
$ echo $?
0

But if I replace the echo with a call to false, I get the desired early exit, but no non-zero exit code:但是,如果我用对 false 的调用替换 echo,我会得到所需的提前退出,但没有非零退出代码:

$ find . -maxdepth 2 -type f \( -exec false {} \; -o \( -printf "FAIL\n" -a -quit \) \)
FAIL
$ echo $?
0

Update:更新:

I am trying to get find to return non-zero when -exec returns false, ie the command that executed returned non-zero.-exec返回false时,我试图让find返回非零,即执行的命令返回非零。 Currently, find just converts the non-zero -exec call into a boolean state to be used as part of a find expression.目前, find只是将非零-exec调用转换为布尔状态,以用作find表达式的一部分。

$ find . -maxdepth 2 -type f \( -exec chmod a+x {} \; -o \( -printf "FAIL\n" -a -quit \) \)

This currently will never return non-zero if the chmod fails.如果 chmod 失败,这当前将永远不会返回非零值。 I want to be able to return non-zero if the chmod fails, as well as exit early, which it already does using -quit .如果 chmod 失败,我希望能够返回非零值,以及提前退出,它已经使用-quit

If you always want to return a non-zero exit code, use && false as shown below:如果您总是想返回非零退出代码,请使用&& false ,如下所示:

find . -maxdepth 2 -type f ...  && false

Use grep to look for the the special FAIL string printed by find .使用grep查找find打印的特殊FAIL字符串。 It will return zero if the exec failed, non-zero otherwise.如果exec失败,它将返回零,否则返回非零。

$ find . -maxdepth 2 -type f \( -exec chmod a+x {} \; -o \( -printf "FAIL\n" -a -quit \) \) | grep -q "FAIL"

I think it is not a good idea to abuse the exit code of find to state a result.我认为滥用find的退出代码来声明结果并不是一个好主意。 It will prevent find 's standard way of reporting errors (eg I/O errors, permission denied errors, etc.).它将阻止find报告错误的标准方式(例如 I/O 错误、权限被拒绝错误等)。

I think you really should use some kind of output of find to state the result and check for that.我认为你真的应该使用find的某种输出来说明结果并检查它。

rm ./findResult 2>/dev/null
find /path/to/my/dir -name '*my*pattern*' -fprintf ./findResult "fail" -quit
result=$(cat ./findResult 2>/dev/null || echo "success")

This way you have a general way of returning values (not just 8 bit int but arbitrary strings).这样,您就有了一种返回值的通用方式(不仅仅是 8 位 int,而是任意字符串)。 Using the exit code, on the other hand, will even cloak errors detected by find .另一方面,使用退出代码甚至会掩盖find检测到的错误。

find has the "plus" version of the -exec command. find具有-exec命令的“加号”版本。 These actually exit with non-zero status, if one or more invocations of the command fail.如果命令的一个或多个调用失败,它们实际上以非零状态退出。 So:所以:

find . -maxdepth 2 -type f -exec false "{}" "+"

will give you the desired "fail" exit status.将为您提供所需的“失败”退出状态。 But it will execute your command (here false ) only once, with all files found listed as arguments.但它只会执行一次您的命令(此处为false ),找到的所有文件都列为参数。 So you need to use a command, that can handle a list of files, and that will exit a non-zero status, if processing any of the provided files failed.因此,您需要使用一个可以处理文件列表的命令,如果处理任何提供的文件失败,它将退出非零状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM