notes/GPS-Distance-in-Kilometers.sh
#!/bin/bash
#Calculate the distance between two geographic coordinates points (latitude longitude)
#code by mikhail - http://tinyurl.com/y5vsxzvx

if [ "$#" -lt 4 ];then
  cat <<EOF
# input:
$0 -22.9077611 -43.1259322 -22.9319733 -43.1947925

# output:
7.5510

# So, the distance between -22.9077611,-43.1259322 and -22.9319733,-43.1947925 is 7.5510 Km
EOF

exit 1
fi

km="$(echo $@|awk '{d($1,$2,$3,$4);} function d(x,y,x2,y2,a,c,dx,dy){dx=r(x2-x);dy=r(y2-y);x=r(x);x2=r(x2);a=(sin(dx/2))^2+cos(x)*cos(x2)*(sin(dy/2))^2;c=2*atan2(sqrt(a),sqrt(1-a)); printf("%.4f",6372.8*c);} function r(g){return g*(3.1415926/180.);}')"

echo "${km} kilometers"

if [ ! -f '/usr/bin/units' ];then
  echo "units needed to get miles - apt-get install units"
else
  units -v ${km}km miles|head -n 1|awk '{print $3 " " $4}'
fi


syntax highlighted by Code2HTML, v. 0.9.1