Why grep fails
Doing this in a terminal:
Fails - Waiting for input
$ grep "index123"
Works - Receives input from STDIN
$ echo -e "line1\nline2\nindex123blah\n" | grep "index123"
index123blah
The 2nd one works because we presented input for grep
to parse via STDIN. grep
will take input from either STDIN or a file. In your scenario:
if [[ "$d"=="*.out" && grep "index123"]]; then exit 1; fi
It's not parsing either, hence it's failing.
Your issue
From the looks of your code I believe you want something like this. To start here's some sample data:
$ mkdir -p 1/2/{3..5}
$ echo "index123" > 1/2/3/blah.out
$ echo "index123" > 1/2/3/blip.out
$ echo "index" > 1/2/blip.out
$ echo "index" > 1/2/blah.out
And a modified form of your script:
$ cat loopy.bash
#!/bin/bash
while IFS= read -r d; do
grep -l "index123" "$d" && exit 1
done < <(find . -type f -name "*.out")
Running:
$ ./loopy.bash
./1/2/3/blah.out
Emits the first file it found that has index123
string, and exits.
While loop a good solution?
I wouldn't do it this way. Using a while
loop here in this manner isn't necessary. It would be better to use a find .... | xargs ...
architecture, or find ... -exec
type of solution.
See @DopeGhoti's answer for a better approach to using find
with either -exec
or xargs
.
Printing containing directory
If you simply want to print the containing directory tree where the matching file resides, you can use dirname
to do this like so:
$ cat loopy2.bash
#!/bin/bash
while IFS= read -r d; do
grep -q "index123" "$d" && dirname "$d" && exit 1
done < <(find . -type f -name "*.out")
And running it:
$ ./loopy2.bash
./1/2/3
==
and before]]
like:[[ "$d" == "*.out" && grep "index123" ]]
. However the second partgrep "index123"
doesn't make sense. Maybe you want[[ "$d" == "*.out" && "$d" =~ index123 ]]
. However overall I'm not sure what you are trying to accomplish. – jesse_b Jul 19 '18 at 19:24grep "index123"
suppose togrep
against? You typically do something likeecho $d | grep -q "index123"
. – slm Jul 19 '18 at 19:29*.out
file should have the text "index123". That is why I usedgrep
. I am trying to find out the *.out file by searching through "index123" and then print the corresponding folder name in the mentionedwhile
loop. – Akand Jul 19 '18 at 19:55grep
to see it. Try it in a shellgrep "index123"
in a terminal will just hang, it's hanging b/c you haven't told it what togrep
. – slm Jul 19 '18 at 20:00grep
from the file "*.out" which should have text "index123". – Akand Jul 19 '18 at 20:05*.out
supposed to match a directory or a file name? And isindex123
supposed to be from the contents of a file or from a filename? – Kusalananda Jul 19 '18 at 20:21index123
to be found? Is it part of a filename or found in the contents of some file. According to your own code,*.out
should match directory names. Why do you use-prune
like you do? – Kusalananda Jul 19 '18 at 20:26index123
should be found in a filename and in the contents of a file? – Kusalananda Jul 19 '18 at 20:27prune
here can be replaced as-maxdepth 1
. – Akand Jul 19 '18 at 20:33