As realpath is not installed per default on my Linux system, the following works for me:
SCRIPT="$(readlink --canonicalize-existing "$0")"
SCRIPTPATH="$(dirname "$SCRIPT")"
$SCRIPT
will contain the real file path to the script and $SCRIPTPATH
the real path of the directory containing the script.
Before using this read the comments of this answer.