Find command doesn't pipe the output as required.

Hi,

I am using below code snippet to echo/display the files found
(matching a pattern from searchstring.out file) and the corresponding owner.

while read j
do
echo "Pattern to search is:- $j"
  find / -name "*$j*" |\
  while read k
  do
    echo "File found is:- $k"
    owner=$(ls -ld "$k" | awk '{print $3}')
    echo "Owner is:- $owner"
  done
done < searchstring.out

However, this is not working as expected. The pattern is getting "echoed" correctly (as Pattern to search) but it doesn't proceeds ahead. No file found is displayed. Whereas, when I execute the find manually on shell using the search string, there are rows returned from the same.

Can anyone please confirm/suggest what could be the issue here and how this can be resolved?

Many Thanks.

The last line should probably read done > searchstring.out , that is redirecting the output.

Hi Walter,

The last line is reading the strings/pattern to find from the out file rather, hence its < - which is correct I believe.

Regards.

I see.

Strangely trying your example as is works for me. Are you using a different shell for the script than on the command line? In any case doing something like bash -x scriptname may help with debugging.

Hi Walter,

I am running this on HP-UX box (k shell). It doesn't really work for me and I am not understanding why :frowning:

Ok, so where does the No file found message come from? Sounds like it would be find (linux find doesn't do this btw.). Or isn't that the exact error message?

Hi,

Ok, so there is a confusion I believe. What I meant is:- The pattern from the out file (searchstring.out) is displayed on console but the output from the find command (files found) is not returned with the next echo.

So, it essentially means that after the pattern is echoed from the out file, there is not further output from the next echo and the execution is stuck after the pattern is displayed from first echo in the script.

Hope this helps.

Regards.

Is it stuck or does it come back with no result? Will any of the files exactly match an entry in the searchstring file?

The execution gets stuck after first echo (which displays the string to be matched from the out file).
When the find command is executed on shell, it indeed returns the results (matching files) but this is not the case when I run via script. So, I concluded that find is the one creating a problem while piping output.

This is probably the difference between line buffering on the shell, and block buffering when the result is piped. To test the theory, just pipe the find through cat in the shell. It will probably hang for quite a while too. So either your find doesn't really terminate because it searches exhaustive nfs mounted volumes or just giving it some more time might help.