Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 将“查找”命令转换为函数,以便在PDF文件中搜索_Linux_Bash_Shell - Fatal编程技术网

Linux 将“查找”命令转换为函数,以便在PDF文件中搜索

Linux 将“查找”命令转换为函数,以便在PDF文件中搜索,linux,bash,shell,Linux,Bash,Shell,我有以下命令取自 我想把它变成一个函数 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_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.

向函数传递参数的语法是正确的(请参阅)。但问题在于单引号中
$1
变量的扩展。请阅读接受的答案

因此,在解决单引号问题后,您的代码可能如下所示:

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

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

pdf_search "your text"