- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
好的,所以我有一个脚本来处理 find
的空分隔输出,我可以像这样使用 bash shell 轻松处理它:
#!/bin/sh
find "$1" -print0 | while read -rd '' path; do echo "$path"; done
相当愚蠢的示例,因为它只是将结果转换为换行符,但这只是为了让您了解我想要做什么。这种基本方法效果很好,并且避免了由于各种文件系统上的文件可能包含换行符而导致的潜在问题。
但是,我需要在非 bash shell 上做同样的事情,这意味着我失去了对 read -d
的支持。那么,在不求助于 bash(或其他 shell)特定功能的情况下,有没有一种方法可以像上面那样处理以 null 分隔的结果?
如果不是,什么是最好的保护自己免受结果换行的方法?我在想我也许可以使用 find
的 -exec
选项用某种转义值替换文件名中的换行符,但我不确定查找和替换新行的最佳方法(例如,我不能使用 tr
)或使用什么替换,这就是为什么空字符是最好的选择(如果可用)。
最佳答案
参见 How can I find and safely handle file names containing newlines, spaces or both? .
例如,您可以使用 find -exec
:
find [...] -exec <command> {} \;
或xargs -0
:
find [...] -print0 | xargs -r0 <command>
请注意,在上面的示例中,您仍然需要设置 IFS
否则您将删除前导/尾随空格:
while IFS= read -rd '' file; do
do_something_with "${file}"
done
你是对的,这个read
只能在bash
中正常工作,这真是太可惜了。我通常不会在意文件名中可能的换行符,只是确保可移植代码在出现时不会中断(而不是忽略问题和脚本爆炸),我认为这足以满足大多数情况,例如
while IFS= read -r file; do
[ -e "${file}" ] || continue # skip over truncated filenames due to newlines
do_something_file "${file}"
done < <(find [...])
或使用 globbing
(如果可能)其行为正确:
for file in *.foo; do
[ -e "${file}" ] || continue # or use nullglob
do_something_file "${file}"
done
关于bash - 如何在非 Bash Shell 中迭代 Null 分隔结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22762816/
我是一名优秀的程序员,十分优秀!