Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

BASH Find Exec Custom Function

I defined the following BASH function to count the number of reads in fastq:

fastq_countGzReads() { zcat "$1" | echo $((`wc -l`/4)) ; }

Now I want to apply this function to a number of gz files found with find.

find . -iname "*gz" -exec sh fastq_countGzReads {} \;


Error:
sh cannot open fastq_countGzReads

How to apply a BASH function on a selection of gz files in a directory ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

A web search on bash find exec function should bring up several hits that cover various ways to slice-n-dice this.

One idea:

export -f fastq_countGzReads          # export the function so it can be
                                      # referenced in a follow-on subshell

### and now a tweak to OP's find/-exec

find . -iname "*gz" -exec bash -c 'fastq_countGzReads {}' \;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading