Category: Bash

  • Bash Snippet: Calculating the Distance Between 2 Coordinates

    Bash Snippet: Calculating the Distance Between 2 Coordinates

    I have a tendency to do things in bash that I’d probably be better off doing in perl or python. Although bash may have super powers, math is not one of them, and so like my last post this script also requires bc. I’ll try and keep these code snippet posts short and sweet, and yes this is my feeble attempt to pad my year-end post numbers.

    Calculate the distance between two longitude and latitude points in bash:

    deg2rad () {
            bc -l <<< "$1 * 0.0174532925"
    }

    rad2deg () {
            bc -l <<< "$1 * 57.2957795"
    }

    acos () {
            pi="3.141592653589793"
            bc -l <<<"$pi / 2 – a($1 / sqrt(1 – $1 * $1))"
    }

    distance () {
            lat_1="$1"
            lon_1="$2"
            lat_2="$3"
            lon_2="$4"
            delta_lat=`bc <<<"$lat_2$lat_1"`
            delta_lon=`bc <<<"$lon_2$lon_1"`
            lat_1="`deg2rad $lat_1`"
            lon_1="`deg2rad $lon_1`"
            lat_2="`deg2rad $lat_2`"
            lon_2="`deg2rad $lon_2`"
            delta_lat="`deg2rad $delta_lat`"
            delta_lon="`deg2rad $delta_lon`"

            distance=`bc -l <<< "s($lat_1) * s($lat_2) + c($lat_1) * c($lat_2) * c($delta_lon)"`
            distance=`acos $distance`
            distance="`rad2deg $distance`"
            distance=`bc -l <<< "$distance * 60 * 1.15078"`
            distance=`bc <<<"scale=4; $distance / 1"`
            echo $distance
    }

    # Keywest, FL to Cuba
    distance 23.137160859692981.68746948242188 24.54079389806876781.76849365234375

    Edit: Top Google result for “calculate the distance between two coordinates in bash” isn’t even done in bash, but rather compiled C code called from a bash script. Seriously Dave Taylor, you have a series of articles at Linux Journal called “Work the Shell”, and this is what you came up with? At least I tried.

  • Bash Snippet: HTML &#code; decoder

    Bash Snippet: HTML &#code; decoder

    A short and simple way to decode HTML decimal (and hex) character codes in bash.

    html_decode () {
        html_encoded="$1"
        html_encoded=${html_encoded//&#/ }
        html_encoded=(`echo $html_encoded`)
        for html_dec in ${html_encoded[@]}
        do
            html_dec="${html_dec//X/x}"
            html_dec="${html_dec//;/}"
            if [ "${html_dec:0:1}" == "x" ]; then
                html_hex=${html_hex:1:${#html_hex}}
            else
                html_hex="`printf "%02X\n" $html_dec`"
            fi
            echo -en "\x$html_hex"
        done
        echo ""
    }

    html_decode "&#73;&#100;&#97;&#104;&#111;"

    On a side note, I’m totally bummed that the builtin ‘printf’ does decimal to hexadecimal conversion negating the need for my much uglier solution:

    dec2hex () {
    num="$1"
    base16=(0 1 2 3 4 5 6 7 8 9 A B C D E F)
    while [ "$num" -gt 0 ];
    do
            hex=${base16[$(($num % 16))]}$hex
            num=$(($num / 16))
    done
    echo "$hex"
    }
    user@host:~$ dec2hex 48879
    BEEF
    echo $((16#BEEF))
    user@host:~$ 48879