bash 如何让 CURL 保存到不同的目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/736225/
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-17 20:48:28  来源:igfitidea点击:

How to get CURL to save to a different directory?

bashcurl

提问by Geuis

I need to be able to pass in the URL of the file download, plus a path for the file to be saved to.

我需要能够传入文件下载的 URL,以及要保存到的文件的路径。

I think it has something to do with -O and -o on CURL, but I can't seem to figure it out.

我认为这与 CURL 上的 -O 和 -o 有关,但我似乎无法弄清楚。

For example, this is what I'm using in my bash script now:

例如,这就是我现在在 bash 脚本中使用的内容:

#!/bin/sh

getsrc(){
    curl -O 
}

getsrc http://www.apache.org/dist/ant/binaries/apache-ant-1.7.1-bin.tar.gz

How can I change the curl statement so I can do

如何更改 curl 语句以便我可以执行

getsrc http://www.apache.org/dist/ant/binaries/apache-ant-1.7.1-bin.tar.gz /usr/local

and have it save the file to /usr/local?

并将文件保存到/usr/local?

采纳答案by Varkhan

Hum... what you probably want to do is

嗯……你可能想做的是

getsrc(){
    ( cd  > /dev/null ; curl -O  ; ) 
}

The -O (capitalO) says to store in a local named like the remote file, but to ignore the remote path component. To be able to store in a specific directory, the easiest way is to cd to it... and I do that in a sub-shell, so the dir change does not propagate

-O(大写O)表示存储在类似于远程文件的本地名称中,但忽略远程路径组件。为了能够存储在特定目录中,最简单的方法是 cd 到它...我在子 shell 中这样做,因此目录更改不会传播

回答by lhunath

If this is to be a script, you should make sure you're ready for any contingency:

如果这是一个脚本,您应该确保您已准备好应对任何突发事件:

getsrc() {
    ( cd "" && curl -O "" )
}

That means quotingyour parameters, in case they contain shell metacharacters such as question marks, stars, spaces, tabs, newlines, etc.

这意味着引用您的参数,以防它们包含 shell 元字符,例如问号、星号、空格、制表符、换行符等。

It also means using the &&operator between the cdand curlcommands in case the target directory does not exist (if you don't use it, curl will still download without error but place the file in the wrong location!)

这也意味着在目标目录不存在的情况下使用and命令&&之间的操作符(如果你不使用它,curl 仍然会下载而不会出错,但将文件放在错误的位置!)cdcurl

That function takes two arguments:

该函数有两个参数:

  • The URL to the data that should be downloaded.
  • The local PATH where the data should be stored (using a filename based off of the URL)
  • 应下载的数据的 URL。
  • 应存储数据的本地 PATH(使用基于 URL 的文件名)

To specify a local filename rather than a path, use the more simplistic:

要指定本地文件名而不是路径,请使用更简单的:

getsrc() {
    curl "" > ""
}