相关疑难解决方法(0)

为什么循环查找的输出是不好的做法?

这个问题的灵感来自

为什么使用 shell 循环处理文本被认为是不好的做法?

我看到这些结构

for file in `find . -type f -name ...`; do smth with ${file}; done
Run Code Online (Sandbox Code Playgroud)

和

for dir in $(find . -type d -name ...); do smth with ${dir}; done
Run Code Online (Sandbox Code Playgroud)

几乎每天都在这里使用,即使有些人花时间对这些帖子发表评论,解释为什么应该避免这种东西......
看到此类帖子的数量(以及有时这些评论被简单地忽略的事实)我想我还不如问一个问题:

为什么循环find的输出是不好的做法,为 返回的每个文件名/路径运行一个或多个命令的正确方法是find什么?

find filenames files for

193
推荐指数
4
解决办法
3万
查看次数

在参数扩展中转义特殊字符

file=/tmp/text/foo.txt
Run Code Online (Sandbox Code Playgroud)

现在我想/bar使用参数扩展语法在此目录结构中添加一个子文件夹。这条线的工作原理:

echo "${file%/*}/bar/${file##*/}"
Run Code Online (Sandbox Code Playgroud)

现在我想知道是否可以通过${parameter/%pat/string}语法使其更短。我想制作/为pat,并将其替换为/bar/。但我不知道如何告诉系统哪个/是拍拍部分。我试图用 来逃避它\,但没有成功。我写的代码是:

echo "${f/%///bar/}"
Run Code Online (Sandbox Code Playgroud)

或者

echo "${f/%\///bar/}"
Run Code Online (Sandbox Code Playgroud)

但它们都不起作用。

zsh shell-script

5
推荐指数
1
解决办法
441
查看次数

标签 统计

filenames ×1

files ×1

find ×1

for ×1

shell-script ×1

zsh ×1