Last active
June 5, 2019 13:33
-
-
Save AndersDJohnson/4243371 to your computer and use it in GitHub Desktop.
bash script BASEDIR
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# from: https://github.com/zimbatm/direnv/blob/3bb35d375331fca89614f0015b2a6cd21688ab79/bin/direnv | |
# usage: abs_dirname $filename | |
# finds the original $filename path and prints it's absolute folder path | |
abs_dirname() { | |
prev_path="$1" | |
# Resolve the symlink(s) recursively | |
while true; do | |
abs_path=`readlink "$prev_path"` | |
if [ -z "$abs_path" ]; then | |
abs_path="$prev_path" | |
break | |
else | |
prev_path="$abs_path" | |
fi | |
done | |
unset prev_path | |
# Get the absolute directory of the final $abs_path | |
orig_dir=`pwd` | |
cd `dirname "$abs_path"` | |
# prints an absolute path here | |
pwd | |
cd "$orig_dir" | |
unset abs_path orig_dir | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
CURDIR=${PWD} | |
BASEDIR="$( cd "$( dirname "$(realpath $BASH_SOURCE)" )" && pwd )" | |
# This didn't support symlinked scripts | |
# BASEDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
BASEDIR=$(dirname "$(readlink -f $0)") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment