我目前正在处理可更新文件的脚本。该文件有多个版本。重要的链接具有静态链接。这些文件将重定向到实际文件,然后使用下载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)
我的问题是我必须使用什么标志来不下载文件,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
$ _
这里:
-s
防止curl
显示进度条;-I
品牌curl
发出HEAD请求;-L
进行curl
跟随重定向(感谢@brianstone),您可能想要或不希望包含此重定向,具体取决于您要跟踪的重定向标头;本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句