Google search rank – Shell Script

Below you can find a script I have written to track my domain name ranking on google search using shell scripts. The script uses keywords strings as a parameter to run, but if you don’t provide any, you still can enter your keywords after running the script.

The script will generate a text file containing the list of URLs extracted from the search result.

Please remember that this script can be customized to allow you even finding which page your result is in by playing with the GET att start in the google URL (0 = page 1 | 10 = page 2| 20 = page 3| etc).

#!/bin/bash

clear
echo ""
echo ".=========================================================."
echo "|                                                         |"
echo "|          Find your domain rank on Google                |"
echo "|  ---------------------------------------------------    |"
echo "|                                                         |"
echo "|  Developer: Achraf Ammar                                |"
echo "|  Usage: ./googlesearch.sh <search strings>              |"
echo "|  Example: ./googlesearch.sh Linux/Unix                  |"
echo "|                                                         |"
echo ".=========================================================."
echo ""

LC_CTYPE=C
unset LANG

if [ -z $1 ]
then
echo "ERROR: No search string supplied as a parameter."
echo "USAGE: ./googlesearch.sh <search srting>"
echo ""
echo -n "Anyways for now, supply the search string here: "
read SEARCH
else
SEARCH=$@
fi

URL="http://google.com/search?hl=en&start=0&safe=off&q="
STRING=`echo $SEARCH | sed 's/ /%20/g'`
URI="$URL%22$STRING%22"

lynx -dump $URI > gone.tmp
sed 's/http/\^http/g' gone.tmp | tr -s "^" "\n" | grep http| sed 's/\ .*//g' > gtwo.tmp
rm gone.tmp
sed '/google.com/d' gtwo.tmp > urls
rm gtwo.tmp

echo "SUCCESS: Extracted `wc -l urls` and listed them in '`pwd`/urls' file for reference."
echo ""

echo -n "Enter your domain name: "
read domname

line=0
filename='urls'
echo "Your google ranking results: "
while read p; do
let "line++"
if [[ "$p" =~ "$domname" ]]; then
echo "[ $line ] $p"
fi
done < $filename
#EOF

This is a screenshot of the final result generated by the script. Keywords used were: essex university
Capture d’écran 2016-03-17 à 2.42.45 AM