简体   繁体   English

循环遍历名称中有空格的目录

[英]Looping through directories that have space in their name

I am sorry if someone asked this question gain, but I didn't find a solution which worked for me.如果有人问这个问题,我很抱歉,但我没有找到适合我的解决方案。

I wrote a small bash script, which works with normal folders.我写了一个小的 bash 脚本,它适用于普通文件夹。 It should run a command for every Subfolder.它应该为每个子文件夹运行一个命令。 But if there is a space in a Folder Name, there is a problem.但如果文件夹名称中有空格,则有问题。

If there is a Folder called "New Folder" in /share/Download, the script will try to access to the folder "/share/Download/New" (instead of "New Folder")如果 /share/Download 中有一个名为“New Folder”的文件夹,脚本将尝试访问文件夹“/share/Download/New”(而不是“New Folder”)

i also tried with echo "$d"我也试过 echo "$d"

    for d in `find /share/Download -mindepth 1 -type d`
do
    echo
    echo $d
    curl http://localhost:2345/api/command -X POST -d '{"name": "something", "path": "'"$d"'"}' --header "X-Api-Key:123456789"
    echo
done

Shell is doing word splitting on find 's output and that is the reason you are having the trouble. Shell 正在对find的输出进行分,这就是您遇到问题的原因。

Use process substitution to properly read the output of find :使用进程替换正确读取find的输出:

while read -r d; do
  # your logic
done < <(find /share/Download -mindepth 1 -type d)

Even better, make find emit NUL terminated output so that directories that have a newline in them would be handled appropriately as well:更好的是,使find发出 NUL 终止的输出,以便在其中包含换行符的目录也能得到适当的处理:

while read -r -d '' d; do
  # your logic
done < <(find /share/Download -mindepth 1 -type d -print0)

This answer can help you further.这个答案可以帮助你进一步。

I allready found the problem and a solution after more research.经过更多研究,我已经找到了问题和解决方案。

for d in isn't whitespace save. for d in 不是空格保存。

The following code did the trick:以下代码成功了:

    find  /share/Download -mindepth 1 -type d|while read fname; do
  curl http://localhost:1234/api/command -X POST -d '{"name": "something", "path": "'"$fname"'"}' --header "X-Api-Key:123456789"
done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM