ism*_*ail 22
还有一种更简单的方法
curl -w "%{url_effective}\n" -I -L -s -S $URL -o /dev/null
Run Code Online (Sandbox Code Playgroud)
它会打印
http://raspberrypi.stackexchange.com/questions/1508/how-do-i-access-the-distributions-name-on-the-command-line/1521
Run Code Online (Sandbox Code Playgroud)
网址
http://raspberrypi.stackexchange.com/a/1521/86
Run Code Online (Sandbox Code Playgroud)
Ale*_*ain 19
尝试这个:
$ LOCATION=`curl -I http://raspberrypi.stackexchange.com/a/1521/86 | perl -n -e '/^Location: (.*)$/ && print "$1\n"'`
$ echo "$LOCATION"
/questions/1508/how-do-i-access-the-distributions-name-on-the-command-line/1521#1521
Run Code Online (Sandbox Code Playgroud)
Google 重定向 URL 略有不同。他们返回一个 Javascript 重定向,可以很容易地处理,但为什么不处理原始 URL 和 go curl 一起呢?
$ URL="http://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=0CFAQFjAA&url=http%3A%2F%2Fwww.raspberrypi.org%2F&ei=rv8oUODIIMvKswa4xoHQAg&usg=AFQjCNEBMoebclm0Gk0LCZIStJbF04U1cQ"
$ LOCATION=`echo "$URL" | perl -n -e '/url=([a-zA-Z0-9%\.]*)/ && print "$1\n"'`
$ echo "$LOCATION"
http%3A%2F%2Fwww.raspberrypi.org%2F
$ echo "$LOCATION" | perl -pe 's/%([0-9a-f]{2})/sprintf("%s", pack("H2",$1))/eig'
http://www.raspberrypi.org/
Run Code Online (Sandbox Code Playgroud)
curl可以配置为跟随重定向并在完成后打印变量。因此,您可以使用以下命令来实现您的要求:
curl -Ls -w %{url_effective} -o /dev/null https://google.com
Run Code Online (Sandbox Code Playgroud)
手册页解释了必要的参数,如下所示:
-L, --location Follow redirects (H)
-s, --silent Silent mode (don't output anything)
-w, --write-out FORMAT Use output FORMAT after completion
-o, --output FILE Write to FILE instead of stdout
Run Code Online (Sandbox Code Playgroud)
或者试试这个
curl -s -o /dev/null -I -w "HTTP_CODE: %{http_code}\nREDIRECT_URL: %{redirect_url}\n" http://raspberrypi.stackexchange.com/a/1521/86
Run Code Online (Sandbox Code Playgroud)