Bash 脚本 - 迭代 find 的输出 [英] Bash Script - iterating over output of find
本文介绍了Bash 脚本 - 迭代 find 的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 bash 脚本,我需要在其中遍历 find 命令输出的每一行,但似乎我正在遍历 find 命令中的每个单词(空格分隔).到目前为止,我的脚本如下所示:
I have a bash script in which I need to iterate over each line of the ouput of the find command, but it appears that I am iterating over each Word (space delimited) from the find command. My script looks like this so far:
folders=`find -maxdepth 1 -type d`
for $i in $folders
do
echo $i
done
我希望这会给出如下输出:
I would expect this to give output like:
./dir1 and foo
./dir2 and bar
./dir3 and baz
但我得到这样的输出:
./dir1
and
foo
./dir2
and
bar
./dir3
and
baz
我在这里做错了什么?
推荐答案
由于您没有使用 find
的任何更高级的功能,您可以使用一个简单的模式来遍历子目录:
Since you aren't using any of the more advanced features of find
, you can use a simple pattern to iterate over the subdirectories:
for i in ./*/; do
echo "$i"
done
这篇关于Bash 脚本 - 迭代 find 的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文