【问题标题】:Ag Silver Searcher: output file name when used in while-loopAg Silver Searcher:在while循环中使用时输出文件名
【发布时间】:2017-04-20 05:46:47
【问题描述】:

在 Bash while 循环中使用 agedit:版本 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;

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

【问题讨论】:

  • 看起来很奇怪。你有控制 ag 的别名或函数吗? type -a ag 显示什么?

标签: bash while-loop ag


【解决方案1】:

你可以避免使用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 需要一个选项来强制允许这样做?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-24
    • 1970-01-01
    • 1970-01-01
    • 2017-02-03
    • 2018-04-17
    • 1970-01-01
    • 2012-01-02
    • 2013-09-28
    相关资源
    最近更新 更多