bash curl: (26) 当文件是变量时无法打开文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40591835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 15:23:48  来源:igfitidea点击:

curl: (26) couldn't open file when the file is a variable

bashcurl

提问by sTr8_Struggin

I am trying to upload a list of files to a server. This is the script that I have

我正在尝试将文件列表上传到服务器。这是我的脚本

files=$(shopt -s nullglob dotglob; echo /media/USB/*) > /dev/null 2>&1
if (( ${#files}  ))
    then
            for file in $files
            do
                    echo "Filename"
                    echo $file
                    curl -i -X POST -F files=@$file 192.168.1.122:5000/upload
            done

Basically I am trying to take all of the files on a USB drive and upload them to my local server. The curl command is giving me trouble. I can move these files to drives that I mount on this system but I haven't been able to send them with the curl command. I have tried variations on @"$file"and @\"$file\"based on other related questions but I haven't been able to get this to work. However what is annoying is that when I do this:

基本上,我试图将 USB 驱动器上的所有文件上传到我的本地服务器。curl 命令给我带来了麻烦。我可以将这些文件移动到我在该系统上安装的驱动器,但我无法使用 curl 命令发送它们。我已经尝试过基于其他相关问题@"$file"@\"$file\"基于其他相关问题的变体,但我无法让它发挥作用。然而,令人讨厌的是,当我这样做时:

curl -i -X POST -F files=@/absolute/path/to/my/file.txt 192.168.1.122:5000/upload

It works as I expect. How can I get this to work in my loop?

它按我的预期工作。我怎样才能让它在我的循环中工作?

回答by sTr8_Struggin

So I ended up figuring out a solution that I will share in case anyone else is having this problem. I am not sure exactly why this fixed it but I simply had to put quotes around the files=@$filein the curl command:

所以我最终想出了一个解决方案,如果其他人遇到这个问题,我会分享这个解决方案。我不确定为什么会修复它,但我只需要files=@$file在 curl 命令中将引号括起来:

curl -i -X POST -F "files=@$file" 192.168.1.122:5000/upload

Leaving this here in case it is useful to someone down the line.

留在这里以防万一它对下线的人有用。