在find + xargs grep中获得了退出代码123 [英] Got exit code 123 in find + xargs grep

查看:165
本文介绍了在find + xargs grep中获得了退出代码123的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的剧本

eval "find \\( -type f -a \\( -name '*.h' \\) \\) -print0" | xargs -0 -n100 grep  -f <(echo "stdio")
echo $?

未找到任何内容,退出代码为123.

Nothing is found and the exit code is 123.

如果我进行如下修改

echo "stdio" >.P
eval "find \\( -type f -a \\( -name '*.h' \\) \\) -print0" | xargs -0 -n100 grep <.P
echo $?

找到了某些内容,但退出代码仍为123.

Something is found but the exit code is still 123.

那怎么了?

================================================ ======================

======================================================================

实际上,我只想编写一个小脚本以使find + xargs + grep更容易.例如,
xgrep -e模式1 -e模式2 ... * .c * .h
要执行
查找-名称* .c -o-名称* .h | xargs grep -f<(回显"$ PATTEN1
$ PATTERN2)

使用-f选项而不是-e是为了避免在模式中转义单引号或双引号时遇到麻烦.

Actually I just want to write a small script to make find+xargs+grep easier. For exmaple,
xgrep -e PATTERN1 -e PATTERN2 ... *.c *.h
is to execute
find -name *.c -o -name *.h|xargs grep -f <(echo "$PATTEN1
$PATTERN2")

Use -f option instead of -e is to avoid troubles in escaping single or double quations within the patterns.

#!/bin/bash
#set -e -o pipefail

eval ARGV=($(getopt -l '' -o 'e:li' -- "$@")) || exit 1
for((i=0;i<${#ARGV[@]};i++)) {
    o="${ARGV[$i]}"
    case $o in
    -e)
        i=$((i+1));
        a="${ARGV[$i]}"
        if [ -n "$grep_patterns" ]; then
            grep_patterns="$grep_patterns"$'\n'
        fi
        grep_patterns="$grep_patterns$a"
        ;;
    -i)
        grep_options="$grep_options -i"
        ;;
    -l)
        grep_options="$grep_options -l"
        ;;
    --)
        i=$((i+1));
        break;;
    esac
}

for((;i<${#ARGV[@]};i++)) {
    if [ -n "$find_options" ]; then
        find_options="$find_options -o "
    fi
    find_options="${find_options}-name '${ARGV[$i]}'"
}

cmd="find \\( -type f -a \\( $find_options \\) \\) -print0"
eval "$cmd" | xargs -0 grep $grep_options -f <(echo "$grep_patterns")

推荐答案

123表示任何以非零状态退出的调用".因此,xargs至少运行了grep次(因为您输入的文件太多,以至于它们超出了最大命令行长度(最多限制为100个文件)),并且至少有一次调用是在一组文件上进行的,没有匹配项,导致grep的退出代码为非零(失败).

123 means "any invocation exited with a non-zero status". So xargs ran grep at least twice (because you fed it so many files that they would exceed the maximum command line length, which you limited to 100 files) and at least one of the invocations was on a set of files which contained no matches, which caused the exit code from grep to be nonzero (failure).

也许您应该解释您要完成的工作. eval看起来是多余的,并且双重重定向可能无法实现您想要的功能(grep的标准输入不能同时连接到eval.P的管道).

Perhaps you should explain what you are trying to accomplish. The eval looks superfluous and the double redirection is probably not accomplishing what you want (grep's standard input cannot simultaneously be connected to the pipe from eval and to .P).

如果您想参数化grep的第一个参数,也许可以做类似的事情

If you want to parametrize the first argument to grep, maybe do something like

#!/bin/sh
find -type f -name '*.h' -print0 |
xargs -0 -n100 grep "$1"

...在这里您可以用例如stdio作为第一个参数.

... where you invoke this with e.g. stdio as the first argument.

(还要注意find的简化参数.您只有两个谓词,因此不需要在括号中加上任何内容,然后也可以删除-a.)

(Notice also the much simplified parameters to find. You only have two predicates so there is no need to parenthesize anything, and then the -a can be dropped, too.)

如果有grep调用返回零匹配项,则退出代码仍为123.您可以通过省略-n 100来减少机会(无论如何似乎几乎没有任何作用),但是如果您要绝对地防止它发生,则可以将整个管道馈送到| grep .,如果有任何输出,它将报告成功. (或者,您可以在包装器上运行xargs,如果grep的退出代码为0或1,该包装器总是返回成功,但这更加复杂,即使匹配为零,您也将看到成功" )

The exit code will still be 123 if there are grep invocations which return zero matches. You can reduce the chances by omitting the -n 100 (which hardly seems to serve any useful pupose anyway) but if you want to absolutely prevent it, you can feed the entire pipeline to | grep . which will report success if there was any output. (Or you could run xargs on a wrapper which always returns success if the exit code from grep is either 0 or 1, but that is more complex, and you will then see "success" even in the case of zero matches.)

这篇关于在find + xargs grep中获得了退出代码123的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆