1

Here documents avoid creation of an intermediate, one-time use file. I was hoping to make use of this when getting the full paths of 20 possible files (without using find -o syntax). I expected the following to work:

find | grep -f <<EOF
Controller.java
Main.java
Config.java
EOF

but I get:

grep: option requires an argument -- f

Am I misunderstanding how here documents work? Or does grep -f not play nicely with file descriptors?

I'm also open to solutions with find that can take a list of -name values, but I don't want to have to type -o 20 times.


Best alternative:

cat <<EOF |  xargs --delimiter '\n' --max-args=1 -I% find -iname "*%*"
Controller.java
Main.java
Config.java
EOF

This is intellectually unsatisfying because you're running find on the entire file hierarchy once for each search pattern which won't scale well for large directory hierarchies.

Sridhar Sarnobat
  • 25,183
  • 12
  • 93
  • 106
  • Not totally following, but do you just want `grep -f-` where that tells grep to read a list of patterns from standard in? You'd then also need to give it a list of files in the command string. – stevesliva Sep 06 '17 at 21:49
  • Hmmmm, it's related but I don't think I can pass it via stdin because I need the stdout of the `find` command to be the stdin to the `grep` command. – Sridhar Sarnobat Sep 06 '17 at 21:52

1 Answers1

1

If you're on a system with /proc (eg linux), try:

#!/bin/sh

find . | grep -f /proc/self/fd/3 3<< EOF
Controller.java
Main.java
Config.java
EOF

Or, if your shell supports process substitution (Bourne Shell does not):

#!/bin/bash

find . | grep -f <( cat << EOF
Controller.java
Main.java
Config.java
EOF
)
Sridhar Sarnobat
  • 25,183
  • 12
  • 93
  • 106
William Pursell
  • 204,365
  • 48
  • 270
  • 300
  • The 2nd one is what I would consider the best solution. It works on Mac OS X (which doesn't have `/proc`). And process substitution is wonderful so it's worth forcing oneself of Bourne Shell (which I only use for scripting anyway, not interactive command line usage). I'm becoming a bigger fan of process substitution every day that passes. – Sridhar Sarnobat Sep 06 '17 at 22:14