Bra*_*one 3 shell-script wget http curl
我目前正在开发一个更新文件的脚本。该文件有多个版本。重要的有静态链接。这些被重定向到实际文件,然后使用wget
. 我发现wget
有一个标志可以打印收到的标题。有一个位置列表。标题中的最后一个位置是实际的 URL。我需要得到那个!
我的想法是用来wget -S
获取标题(我需要另一个标志来防止文件下载和创建)。然后使用管道解析行并捕获包含location
. 我想这可以通过使用grep -l -i "location:" | tail -l
. 然后我应该只剩下一行可以轻松解析的行。
所以命令看起来像这样:
# The -??? flag is the one that prevents the file from downloading. (I don't know it)
Location=$(wget -S -??? $URL | grep -l -i "location:" | tail -l)
Run Code Online (Sandbox Code Playgroud)
我的问题是我必须使用什么标志才能不下载文件,wget
或者是否有另一种方法/命令来完成此操作?
你想要的是 HEAD 请求,但wget
不支持;curl
做。
您的发行版很可能curl
在存储库中。
curl -s -I $URL -L | awk '/Location: (.*)/ {print $2}' | tail -n 1
$ URL=http://unix.stackexchange.com/questions/89282/
$ curl -s -I $URL | awk '/Location: (.*)/ {print $2}' | tail -n 1
/questions/89282/how-to-figure-out-where-a-link-gets-redirected
$ _
Run Code Online (Sandbox Code Playgroud)
这里:
-s
防止curl
显示进度条;-I
品牌curl
发出HEAD请求;-L
进行curl
跟踪重定向(感谢@brianstone),您可能想要或不想包含此内容,具体取决于您要跟踪的重定向标头;