2013-10-21 31 views
1

我有一个命令,我想在我的.bashrc中有一个函数。Bash语法字符串插入

从命令行

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黑客,数组扩展,但没有运气。我相信它是可能的,我相信它很容易,但我无法实现它的工作。

+0

你可能有乐趣写这个,但是你有没有考虑正装[pdfgrep](HTTPS ://gitorious.org/pdfgrep)? – kojiro

回答

0

像变量扩展这样的事情只能在双引号内使用,而不是单引号。你尝试过使用双引号吗?

像这样:

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

的问题可能是单引号'围绕pdftotext命令。单引号将防止字符串中出现任何可变的扩展。双引号"可能会带来更多运气。

eg="string of words" 

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

大概是最简单的事:

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

你需要装饰的逻辑只是有点不同于你做了什么:

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

即通过使shell进程外部引号分隔符",shell变量扩展工作,并且用'将搜索变量分隔为一个字符串。

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" \;