处理特殊字符在bash for ... in循环 [英] Handle special characters in bash for...in loop
问题描述
假如我有一个文件列表
file1
"file 1"
file2
一个for ... in循环打破它之间的空白,而不是新行:
a for...in loop breaks it up between whitespace, not newlines:
for x in $( ls ); do
echo $x
done
结果:
file
1
file1
file2
我想对每个文件执行命令。 文件和1以上是不实际的文件。我该怎么办,如果文件名中包含的东西空格或逗号?
I want to execute a command on each file. "file" and "1" above are not actual files. How can I do that if the filenames contains things like spaces or commas?
这是一个有点麻烦,比我想找到-print0 | xargs的-0可以处理,因为我其实希望该命令是这样的转换输入/ file1.jpg ....输出/ file1.jpg所以我需要排列替换过程中的文件名。
It's a little trickier than I think find -print0 | xargs -0 could handle, because I actually want the command to be something like "convert input/file1.jpg .... output/file1.jpg" so I need to permutate the filename in the process.
推荐答案
更新通过OP :这个答案很烂,不应该在上面...... // @乔丹以下的职位应该是公认的答案
UPDATE BY OP: this answer sucks and shouldn't be on top ... @Jordan's post below should be the accepted answer.
<击>一种可能的方式:
one possible way:
ls -1 | while read x; do
echo $x
done
击>
这篇关于处理特殊字符在bash for ... in循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!