如何在&q;find(.)-exec(.){};&bash命令的{}中使用替身字符串? [英] How to substitute string in {} in "find (...) -exec (...) {} ;" bash command?

查看:24
本文介绍了如何在&q;find(.)-exec(.){};&bash命令的{}中使用替身字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用替身在{}中通过bash的"查找"找到字符串?

举个例子,我想在下面?把替身的"in"改成"out":

find . -name "*.in" -exec python somescript.py {} ? ;

即对所有"*.in"文件执行

python somescript.py somefile.in somefile.out

推荐答案

find没有替换功能。您需要调用shell。

find . -name "*.in" -exec sh -c 'python somescript.py "$0" "${0%.in}.out"' {} ;

$0是文件名,${0%.in}删除.in后缀。

或者,在bash(但不是普通sh)中,运行shopt -s globstar以启用递归目录扩展(如果存在没有任何匹配的.in文件的风险),则运行shopt -s nullglob,并使用for循环而不是find

for x in **/*.in; do
  python somescript.py "$x" "${x%.in}.out"
done

这篇关于如何在&q;find(.)-exec(.){};&bash命令的{}中使用替身字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆