for subdir in $( find $dir -type d ); do echo "subdir is $subdir" done
问题是,如果子目录名称包含空格,它们会分开,我该如何解决?
答案 0 :(得分:1)
您可以使用process substitution
:
while IFS= read -d '' -r subdir; do
echo "subdir is $subdir"
done < <(find "$dir" -type d -print0)
答案 1 :(得分:0)
for subdir in $somedir/*/ do
echo "subdir is $subdir"
done
如果你继续遇到空格问题,那么你已经在其他地方搞砸了。
答案 2 :(得分:0)
oldifs=$IFS
IFS=$'\n'
for subdir in $( find $dir -type d ); do
echo "subdir is $subdir"
done
IFS=$oldifs
示例:
$ bash dirnm.sh a
subdir is a
subdir is a/dir 1
subdir is a/dir 2