When using ag
(edit: version 0.33.0) in a Bash while loop I can't make it show me any file names:
#!/bin/bash
cat <<__m | while read mod; do ag --nogroup --filename --ignore=local "^use $mod" ./; done
CGI
CGI::Push
__m
Output:
use 4.08;
use 4.08;
...
When executing it directly on the command line it works:
ag --nogroup --filename --ignore=local "^use CGI" ./
Output:
dir/file/ModA.pm:12:use CGI 4.08;
dir/file/ModB.pm:1:use CGI 4.08;
How can I always get the filenames inline?
You could avoid using while read
:
#!/bin/bash
for mod in CGI CGI::Push ; do
ag --nogroup --filename --ignore=local "^use $mod" ./
done
I believe your problem is actually because of a bug(?) in ag
. In options.c
, it does:
rv = fstat(fileno(stdin), &statbuf);
if (rv == 0) {
if (S_ISFIFO(statbuf.st_mode) || S_ISREG(statbuf.st_mode)) {
opts.search_stream = 1;
}
}
Since you're running under while read
, your stdin
is not a TTY, so opts.search_stream
ends up being set to 1. This causes the opts.print_path
to be set to PATH_PRINT_NOTHING
. Thus, your paths won't print when run this way. Maybe ag
needs an option to forcibly allow this?