Last active
May 21, 2020 17:49
-
-
Save rojenzaman/d4afdd096f035f53b696f6bd8e6d61c9 to your computer and use it in GitHub Desktop.
URL Shortener For Static Web Pages (written by bash shell programming language)
This file contains 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 | |
if [ "$#" -lt 2 ]; then | |
echo "./`basename $0` <write url here> <write your site directory here> [second] [\"title\"] [\"html code or txt\"]"; | |
exit 1; | |
fi | |
url=$1 | |
directory=$2 | |
if [ "$#" -gt 1 ]; then | |
second=$3 | |
if [ -z "$second" ]; then | |
second="0" | |
fi | |
title=$4 | |
html=$5 | |
fi | |
cd $directory; | |
random_url=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 4 | head -n 1); | |
while [ -f "$directory/$random_url" ]; | |
do | |
random_url=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 4 | head -n 1); | |
done | |
mkdir $random_url && cd $random_url; | |
cat > index.html << EOF | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta http-equiv="refresh" content="$second; url=$url" /> | |
<title>$title</title> | |
</head> | |
<body> | |
<h1>redirect to <a href="$url">$url</a></h1><br> | |
$html | |
</body> | |
</html> | |
EOF | |
if [ -z "$title" ]; then title="null"; fi | |
if [ -z "$html" ]; then html="null"; fi | |
echo "url = $url" | |
echo "site directory = $directory" | |
echo "sleep time = $second" | |
echo "title = $title" | |
echo "html code = $html" | |
echo "-------------------------------------------" | |
echo "SHORT URL = http://yourhostname/$random_url" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment