3

在 Bash while 循环中使用ag编辑:版本 0.33.0)时,我无法让它显示任何文件名:

#!/bin/bash
cat <<__m | while read mod; do ag --nogroup --filename --ignore=local "^use $mod" ./; done
CGI
CGI::Push
__m

输出:

use 4.08;
use 4.08;
...

当直接在命令行上执行它时,它可以工作:

ag --nogroup --filename --ignore=local "^use CGI" ./

输出:

dir/file/ModA.pm:12:use CGI 4.08;
dir/file/ModB.pm:1:use CGI 4.08;

我怎样才能总是内联文件名?

4

1 回答 1

6

您可以避免使用while read

#!/bin/bash
for mod in CGI CGI::Push ; do
     ag --nogroup --filename --ignore=local "^use $mod" ./
done

我相信您的问题实际上是由于ag. 在options.c中,它确实:

rv = fstat(fileno(stdin), &statbuf);
if (rv == 0) {
    if (S_ISFIFO(statbuf.st_mode) || S_ISREG(statbuf.st_mode)) {
        opts.search_stream = 1;
    }
}

由于您在 下运行while read,因此您stdin不是 TTY,因此opts.search_stream最终被设置为 1。这导致opts.print_path被设置为PATH_PRINT_NOTHING. 因此,以这种方式运行时不会打印您的路径。也许ag需要一个选项来强制允许这个?

于 2016-12-05T19:54:20.883 回答