我正在编写一个shell脚本,它遍历一组IMAP文件夹.
鉴于:
WORKPATH=/var/mail/mydomain.com/myaccount/
Run Code Online (Sandbox Code Playgroud)
我想列出里面的所有子文件夹.
轻松找到:
for SUB_FOLDER in $(find "$WORKPATH" -type d)
do
echo "SUB_FOLDER: $SUB_FOLDER"
done
Run Code Online (Sandbox Code Playgroud)
但是:其中一些文件夹的名称中包含空格,例如:
/var/mail/mydomain.com/myaccount/.Deleted Mails/
/var/mail/mydomain.com/myaccount/.Junk Mails/
Run Code Online (Sandbox Code Playgroud)
这使得上面的代码打印出来的邻居:
SUB_FOLDER: /var/mail/mydomain.com/myaccount/.Deleted
SUB_FOLDER: Mails/
SUB_FOLDER: /var/mail/mydomain.com/myaccount/.Junk
SUB_FOLDER: Mails/
Run Code Online (Sandbox Code Playgroud)
我已经尝试使用-print0选项并管道到xargs -0,但我没有运气.与IFS一起玩输出比输入更糟糕的东西.
真的可以使用一个提示如何使find只为4个for循环instad生成2个令牌,或者迭代列表的替代方法.
使用while循环:
while read SUB_FOLDER; do
echo "SUB_FOLDER: $SUB_FOLDER";
done < <(find "$WORKPATH" -type d)
Run Code Online (Sandbox Code Playgroud)