Call find with backticks from perl - find: write error: Broken pipe

I call find from perl script as follows:

  my $ one_file = `find $ search_dir -name" \ *. $ refinfilebase.search "-print |  head -n 1`;

If I execute it from the shell, I will not get an error. Also, it returns the correct value in $ one_file, but I get this at the hint:

  find: write error: Broken pipe

Why was that? How can I get rid of this find: write: message Broken pipe message ?

+4
source share
3 answers

You can try this (although I was not able to reproduce your error message using the code you posted, so maybe this version without errors might give you an error message ...):

my $file = `find $search_dir -name "\*.ssf" -print -exec head -n 1 {} \\;`; 

Here is an example of the output I received from the test run:

 ./tmp1.ssf HEADER PROTEIN 21-FEB-11 1PDB 

NTN

+1
source

This โ€œmistakeโ€ is absolutely normal and should be expected.

  • You use the find command, which outputs (potentially) many lines of output.
  • You load this into head , which exits after receiving one line of input.
  • The find tries to write the remaining lines to a pipe that no longer listens ( head is dead).
  • find gives an error.

If you want to get rid of the error, just do:

 my $file = `find .... 2>/dev/null | head -n 1`; 

This will lead to an unpredictable error from the search to your terminal (since neither the backlinks nor the pipe in the head will touch the stderr where this error is printed).

+4
source

Why are you using find and head in backticks when there are perl paths to handle it? I.e:.

I would recommend using backticks when you are absolutely sure of what you are doing, and it seems to me that you are not doing it. Hell, you can make your current attempt simply by doing this:

 my @files = `find $search_dir -name "\*.$refinfilebase.search" -print` my $one_file = $files[0]; 
+3
source

All Articles