从壳卷曲,带空格的路径

时间:2015-12-15 23:57:29

标签: shell curl

我有一个包含空格的文件夹路径。我用反斜线逃离了空间。 curl命令在命令行上运行。如果我在我的脚本中运行它会崩溃,认为文件夹路径的各个部分是单独的参数。

脚本遍历目录并卷曲所有文件夹/文件 代码

while IFS= read -r -d '' file
do
   filepath=$(dirname "$file")
   #escape spaces
   filenew=$(echo $file | sed -e 's/\s/\\ /g')
   curl -k -T "$filenew" --ftp-ssl --ftp-pasv --ftp-create-dirs -u ${username}@${domain}:${pass} $ftpuploadpath  
 done < <(find ${folderpath} -type f -print0)
 echo "$file"

错误

   Transferring Sample_Project/folder\ spaces/test.docx
   0 Sample_Project/folder spaces/test.docx
   curl: Can't open 'Sample_Project/folder\ spaces/test.docx'!
   curl: try 'curl --help' or 'curl --manual' for more information
   curl: (6) Couldn't resolve host 'spaces'

1 个答案:

答案 0 :(得分:1)

行情,人物,报价。使用sed将语法元素(在本例中为反斜杠)添加到数据中绝对没有意义,因为语法解析会在参数扩展发生之前发生。

while IFS= read -r -d '' file; do
   filepath=$(dirname "$file")
   curl -k --ftp-ssl --ftp-pasv --ftp-create-dirs \
     -T "$file" \
     -u "${username}@${domain}:${pass}" \
     "$ftpuploadpath" 
done < <(find "$folderpath" -type f -print0)

请注意:

  • -T "$file"确保将文件名作为单个参数传递给curl。数据中不需要添加文字反斜杠以产生此效果。
  • 总的来说,
  • "${username}@${domain}:${pass}"需要引用,以防止任何这些元素中的任何空格或整数字符通过字符串拆分和全局扩展影响行为(考虑以<space>*结尾的密码;您不希望在命令行中添加本地目录中的文件名列表。)
相关问题