Skip to content

Instantly share code, notes, and snippets.

@dol
Created December 6, 2019 11:14
Show Gist options
  • Save dol/d037e6b612944834a24b0285efe964c4 to your computer and use it in GitHub Desktop.
Save dol/d037e6b612944834a24b0285efe964c4 to your computer and use it in GitHub Desktop.
Get the path to the script
#!/usr/bin/env bash
function ostype() {
unameOut="$(uname -s)"
case "${unameOut}" in
Linux*) machine=Linux;;
Darwin*) machine=Mac;;
CYGWIN*) machine=Cygwin;;
MINGW*) machine=MinGw;;
*) machine="UNKNOWN:${unameOut}"
esac
echo ${machine}
}
function safe_realpath() {
OSTYPE=$(ostype)
if [ -x "$(command -v realpath)" ]; then
#echo "realpath found !"
realpath $1
return
fi
if [ "$OSTYPE" == "Mac" ]; then
# we dont try readlink in Mac since it is different
# readlink (BSD) used in Linux
echo "realpath is not avaialbe emulating it with python (Mac)"
python -c 'import os,sys;print(os.path.realpath("'"$1"'"))'
exit 0
fi
if [ -x "$(command -v readlink)" ]; then
#echo "Using readlink"
readlink -f $1
return
fi
# I am not sure if it comes to this fallback...
echo "realpath is not avaialbe emulating it with python"
python -c 'import os,sys;print(os.path.realpath("'"$1"'"))'
}
SCRIPT=$(safe_realpath "$BASH_SOURCE")
SCRIPT_PATH=$(dirname $SCRIPT)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment