我有以下命令取自 here
find /path -name '*.pdf' -exec sh -c 'pdftotext "{}" - | grep --with-filename --label="{}" --color "your pattern"' \;
我想把它变成一个类似
的函数 pdf_search() { find ./ -name '*.pdf' -exec sh -c 'pdftotext "$1" - | grep --with-filename --label="{}" --color "$@"' \; ; }
虽然我不确定该怎么做,我只是想能够传递一个参数 到使用它的功能,例如
pdf_search 'look for this'
使用命令为
pdf_search() {
find ./ -name '*.pdf' -exec sh -c 'pdftotext "$1" - | grep --with-filename --label="{}" --color "$1"' \; ;
}
给出以下错误-
I/O Error: Couldn't open file '': No such file or directory.
最佳答案
您将参数传递给函数的语法是正确的(参见 details )。但问题在于单引号内的 $1
变量的扩展。请阅读接受的答案 here
因此,在修复单引号问题后,您的代码可能如下所示:
pdf_search() {
find . -name '*.pdf' -exec sh -c 'pdftotext "{}" - | grep --with-filename --label="{}" --color "$1"' -- "$1" \;
}
pdf_search "your text"
关于linux - 将查找命令转换为函数以便在 PDF 文件中搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34426075/