1

我有一个命令,我想在我的 .bashrc 中有一个函数。

从命令行

find . -name '*.pdf' -exec sh -c 'pdftotext {} - | grep --with-filename --label={} --color "string of words" ' \;

将在当前目录的任何 pdf 中找到“单词串”。

尽管一个小时的最佳时间,我真的不能让“字符串”作为字符串变量工作 - 即

eg="string of words"

find . -name '*.pdf' -exec sh -c 'pdftotext {} - | grep --with-filename --label={} --color $eg ' \;

这显然是行不通的,但我已经尝试了各种黑客攻击、数组扩展的组合,但没有运气"/'/\echo我确信它是可能的,而且我确信它很容易,但我无法让它发挥作用。

4

5 回答 5

0

编写一个小的 shell 脚本mypdfgrep并从以下位置调用它find

#/bin/bash

pdftotext "$1" - | grep --with-filename --label "$1" --color "$2"

然后运行

$ chmod +x mypdfgrep
$ find . -name '*.pdf' -execdir /full/path/to/mypdfgrep '{}' "string of words" \;
于 2013-10-22T13:26:48.717 回答
0

变量扩展之类的东西只在双引号内有效,在单引号内无效。您是否尝试过在该 sring 上使用双引号?

像这样:

find . -name "*.pdf' -exec sh -c 'pdftotext {} - | grep --with-filename --label={} --color $eg " \;
于 2013-10-21T23:45:55.803 回答
0

问题可能是命令'周围的单引号。pdftotext单引号将防止它们出现的字符串中的任何变量扩展。使用双引号可能会更幸运"

eg="string of words"

find . -name '*.pdf' -exec sh -c "pdftotext {} - | grep --with-filename --label={} --color $eg " \;
于 2013-10-21T23:47:05.200 回答
0

您需要将逻辑与您所​​做的稍有不同:

eg="string of words"
find . -name '*.pdf' -exec sh -c "pdftotext {} - | \
   grep -H --label={} --color '$eg'" \;

即,通过使shell进程外引号分隔符",shell变量扩展工作,并将搜索变量与分隔符'保留为字符串。

于 2013-10-22T00:41:17.297 回答
0

可能最简单的做法:

find . -name '*.pdf' -exec \
  sh -c 'pdftotext $0 - | grep --with-filename --label=$0 --color "$1"' {} "$eg" \;
于 2013-10-21T23:52:22.573 回答