1

I have the following command taken from here

  find /path -name '*.pdf' -exec sh -c 'pdftotext "{}" - | grep --with-filename --label="{}" --color "your pattern"' \;

And i want to turn it into a function along the lines of

  pdf_search() { find ./ -name '*.pdf' -exec sh -c 'pdftotext "$1" - | grep --with-filename --label="{}" --color "$@"' \; ; }

I'm not sure how to do this though, i just want to be able to pass an argument to the function to use it such as

  pdf_search 'look for this'

using the command as

pdf_search() { 
  find ./ -name '*.pdf' -exec sh -c 'pdftotext "$1" - | grep --with-filename --label="{}" --color "$1"' \; ; 
}

Gives the following error -

I/O Error: Couldn't open file '': No such file or directory.
Community
  • 1
  • 1
baxx
  • 3,956
  • 6
  • 37
  • 75

1 Answers1

1

Your syntax of passing parameters to a function is correct (see details). But the problem is with the expansion of the $1 variable inside single quotes. Please, read accepted answer here

So, after fixing single quotes problem, your code may look like this:

pdf_search() {
    find . -name '*.pdf' -exec sh -c 'pdftotext "{}" - | grep --with-filename --label="{}" --color "$1"' -- "$1" \;
}

pdf_search "your text"
vrs
  • 1,922
  • 16
  • 23