如何在“find (...) -exec (...) {} ;"中替换 {} 中的字符串bash 命令? [英] How to substitute string in {} in "find (...) -exec (...) {} ;" bash command?
问题描述
如何替换 {}
中由 bash 的find"找到的字符串?
How can I substitute a string in {}
as found by bash's ´find´?
例如,我希望下面的 ?
将in"替换为out":
For instance I would like ?
below to substitute "in" by "out":
找到 .-name "*.in" -exec python somescript.py {} ?;
即对所有*.in"文件执行
i.e. to execute for all "*.in" files
python somescript.py somefile.in somefile.out
推荐答案
find
没有替换功能.你需要调用一个shell.
find
doesn't have a substitution feature. You need to call a shell.
find . -name "*.in" -exec sh -c 'python somescript.py "$0" "${0%.in}.out"' {} ;
$0
是文件名,${0%.in}
去掉.in
后缀.
或者,在 bash(但不是普通 sh)中,运行 shopt -s globstar
以启用递归目录扩展(如果存在以下风险,则运行 shopt -s nullglob
不会有任何匹配的 .in
文件)并使用 for
循环而不是 find
.
Alternatively, in bash (but not in plain sh), run shopt -s globstar
to enable recursive directory expansion (and shopt -s nullglob
if there's a risk that there won't be any matching .in
file) and use a for
loop instead of find
.
for x in **/*.in; do
python somescript.py "$x" "${x%.in}.out"
done
这篇关于如何在“find (...) -exec (...) {} ;"中替换 {} 中的字符串bash 命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!