挣扎着卷曲和打击

时间:2017-08-05 09:26:02

标签: bash

有一个bash脚本,用于将粘贴上传到您拥有并运行的pastebin服务器,具体如下:

haste() { a=$(cat); curl -X POST -s -d "$a" http://example.com/documents | awk -F '"' '{print "http://example.com/"$4}'; }

但是,在更新网址后尝试在我的.bashrc中使用它时,我会收到与以下内容类似的错误:

user@domain:~$ curl -X POST -d 'test' example.com /home/user/test.txt https://example.com | awk -F '"' '{print "https://example.com/"$4}'
% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
Dload  Upload   Total   Spent    Left  Speed
100    18  100    14  100     4    127     36 --:--:-- --:--:-- --:--:--   128
chttps://example.com/
url: (3) <url> malformed
100    18  100    14  100     4     33      9 --:--:-- --:--:-- --:--:--    40

我尝试过这方面的变体,例如省略-X-s以及修改$4变量。坦率地说,我不知道我在这里做错了什么。

我也一直试图将其解决到我刚才做的事情:

curl -F file=@/home/user/example.txt example.com 

但我不确定这是否可行。

是否有可以执行此操作的Pastebin服务,我可以运行?

编辑:越来越近了。排序。

user@example:~$ curl  -X POST example.txt -d 'test' https://example.com | awk -F '"' '{print "https://example.com/"$4}'
% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
Dload  Upload   Total   Spent    Left  Speed
0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     
0curl: (6) Could not resolve host: example.txt
100    18  100    14  100     4     31      8 --:--:-- --:--:-- --:--:--    70
https://example.com/

似乎我应该在这里使用-F位作为example.txt?

1 个答案:

答案 0 :(得分:1)

上述所有示例中的curl命令行语法似乎都有问题。回想一下,从根本上说,curl的命令行看起来像curl [<options>] url1 url2 ... urln。也就是说,任何不是选项或选项参数的东西都被视为URL。所以如果你有:

curl -X POST -d 'test' example.com /home/user/test.txt https://example.com 

您有三个非选项参数:example.com/home/user/test.txthttps://example.com。这就是为什么你看到curl尝试三次获取网址的原因:

第一个:

100    18  100    14  100     4    127     36 --:--:-- --:--:-- --:--:--   128

第二个:

curl: (3) <url> malformed

第三个:

100    18  100    14  100     4     33      9 --:--:-- --:--:-- --:--:--    40

“格式错误的网址”错误来自于尝试抓取/home/user/test.txt

$ curl /home/user/test.txt
curl: (3) <url> malformed

您没有说明您的问题中正在运行哪种pastebin服务,但如果它是haste,那么您的curl命令应如下所示:

curl --data-binary @/home/user/test.txt -X POST https://hastebin.com/documents

以上将文件/home/user/test.txt发送到hastebin。 @<filename>语法是告诉curl从文件中读取数据的方法;有关更多信息,请参见curl手册页。上面的返回值是包含新文档密钥的JSON文档:

{"key":"dudidadoma"}