Skip to content

Instantly share code, notes, and snippets.

@mtk-f
Last active July 11, 2016 05:04
Show Gist options
  • Save mtk-f/0a835db742e605a100b0243814454b87 to your computer and use it in GitHub Desktop.
Save mtk-f/0a835db742e605a100b0243814454b87 to your computer and use it in GitHub Desktop.
短縮URLのリンク先を調べるbashスクリプト
#!/usr/bin/bash
tmp=/tmp/$$
function getLocation() {
curl -I --silent $1 | while read line
do
if [[ "$line" =~ ^HTTP ]]; then
echo $line > $tmp-status
fi
if [[ "$line" =~ ^[Ll]ocation: ]]; then
echo $line > $tmp-location
fi
done
if [ ! -e $tmp-location ]; then
echo $1
return
fi
location=$(echo $(cat $tmp-location) | sed -e "s/^[Ll]ocation: \?//")
rm -f $tmp-location
status=$(cat $tmp-status)
rm -f $tmp-status
if [[ "$status" =~ ^HTTP/.*\.*.*\ 30. ]]; then
getLocation $location
return
fi
echo $location
}
getLocation $1
rm -f $tmp-*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment