Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am calling find from a perl script like this:

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

If I execute it from the shell, I get no error. Also, it returns the right value to $one_file, but I get this on the prompt:

find: write error: Broken pipe

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

like image 229
719016 Avatar asked Feb 24 '23 16:02

719016


1 Answers

This "error" is perfectly normal and is to be expected.

  • You're running a find command which prints out (potentially) many lines of output.
  • You're feeding that into head which quits after getting one line of input.
  • The find command tries to write its remaining lines to a pipe that has no one listening anymore (head is dead).
  • find will throw an error.

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

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

This will keep the utterly predictable error from find from getting to your terminal (since neither the backticks nor the pipe into head touch stderr, which is where that error is being printed).

like image 118
unpythonic Avatar answered Mar 05 '23 16:03

unpythonic