如何让bash find exec直接执行命令而不是使用临时文件?

问题描述 投票:0回答:1

我写了这个 BASH 脚本:

find ./build/html -name '*.html' \( -exec echo ../emojize_pngorsvg.py \"{}\" \> \"{}.emojized\" \&\& rm \"{}\" \&\& mv \"{}.emojized\" \"{}\" >> ../emojize_commands.sh \; \)
cat ../emojize_commands.sh
chmod +x ../emojize_commands.sh
../emojize_commands.sh

它循环遍历在

./build/html
目录中找到的所有 HTML 文件并创建
emojize_commands.sh
,如下所示:

../emojize_pngorsvg.py "./build/html/Home/Technical/Guides/Windows/Force-activate-a-Windows-evaluation/index.html" > "./build/html/Home/Technical/Guides/Windows/Force-activate-a-Windows-evaluation/index.html.emojized" && rm "./build/html/Home/Technical/Guides/Windows/Force-activate-a-Windows-evaluation/index.html" && mv "./build/html/Home/Technical/Guides/Windows/Force-activate-a-Windows-evaluation/index.html.emojized" "./build/html/Home/Technical/Guides/Windows/Force-activate-a-Windows-evaluation/index.html"
../emojize_pngorsvg.py "./build/html/Home/Technical/Guides/Windows/Various-Windows-Tips/index.html" > "./build/html/Home/Technical/Guides/Windows/Various-Windows-Tips/index.html.emojized" && rm "./build/html/Home/Technical/Guides/Windows/Various-Windows-Tips/index.html" && mv "./build/html/Home/Technical/Guides/Windows/Various-Windows-Tips/index.html.emojized" "./build/html/Home/Technical/Guides/Windows/Various-Windows-Tips/index.html"
../emojize_pngorsvg.py "./build/html/Home/Technical/Guides/Windows/Configure-RDP-to-connect-with-stored-credentials/index.html" > "./build/html/Home/Technical/Guides/Windows/Configure-RDP-to-connect-with-stored-credentials/index.html.emojized" && rm "./build/html/Home/Technical/Guides/Windows/Configure-RDP-to-connect-with-stored-credentials/index.html" && mv "./build/html/Home/Technical/Guides/Windows/Configure-RDP-to-connect-with-stored-credentials/index.html.emojized" "./build/html/Home/Technical/Guides/Windows/Configure-RDP-to-connect-with-stored-credentials/index.html"
# Much more lines here

然后它执行

emojize_commands.sh
- 完全符合预期!

我正在尝试消除使用临时文件来完成所有这些操作的需要。我宁愿它直接执行命令,而不需要先将它们

echo
写入文件。

我尝试删除

echo
>> ../emojize_commands.sh
(以及所有其他代码行),所以我只剩下这个:

find ./build/html -name '*.html' \( -exec ../emojize_pngorsvg.py \"{}\" \> \"{}.emojized\" \&\& rm \"{}\" \&\& mv \"{}.emojized\" \"{}\" \; \)

但是当我运行它时,我收到一堆“文件未找到”错误!

我怎样才能让

find
按照我想要的方式工作?

bash find sh refactoring exec
1个回答
0
投票

生成一个 shell 并在那里运行这些命令。

find ./build/html -name '*.html' -exec sh -c '
for f; do
  ../emojize_pngorsvg.py "$f" >"$f.emojized" &&
  mv "$f.emojized" "$f"
done' sh {} +
© www.soinside.com 2019 - 2024. All rights reserved.