我有一个包含空格的文件夹路径。我用反斜杠转义了空格。 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'
最佳答案
语录、人物、语录。使用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}"
需要作为一个整体被引用,以防止这些元素中的任何空格或 glob 字符通过字符串分割和 glob 扩展影响行为(考虑以<space>*
结尾的密码;您不需要文件名列表在添加到命令行的本地目录中)。
关于shell - 来自 shell 的 curl ,带空格的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34301697/