Created
January 6, 2025 12:53
-
-
Save vijinho/310e9dc6ed8bb815eb821668d2611bee to your computer and use it in GitHub Desktop.
bash script to follow a given url to its final destination, i.e. to unshorten URLs or follow-through redirects to the ultimate destination URL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Function to parse command line arguments | |
parse_args() { | |
while [[ $# -gt 0 ]]; do | |
case $1 in | |
-u=*|--url=*) | |
URL="${1#*=}" | |
shift # past argument=value | |
;; | |
*) | |
echo "Invalid option: $1" | |
exit 1 | |
;; | |
esac | |
done | |
if [ -z "$URL" ]; then | |
echo "Error: --url is required." | |
echo "Usage: $0 --url <URL>" | |
exit 1 | |
fi | |
} | |
# Function to get the final URL after following redirects | |
get_final_url() { | |
if ! FINAL_URL=$(curl -Ls -o /dev/null -w %{url_effective} "$URL"); then | |
echo "Error fetching final URL for: $URL" | |
exit 1 | |
fi | |
echo "$FINAL_URL" | |
} | |
# Main script execution | |
parse_args "$@" | |
get_final_url |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment