Created
May 12, 2020 04:38
-
-
Save alvis/d554cd43ad8bebc62b03602963edb88d to your computer and use it in GitHub Desktop.
A procedure for getting the resolved absolute path of current file and its containing directory
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/sh | |
CWD=`pwd -P` | |
TARGET_FILE=$0 | |
cd `dirname $TARGET_FILE` | |
TARGET_FILE=`basename $TARGET_FILE` | |
# iterate down a (possible) chain of symlinks | |
while [ -L "$TARGET_FILE" ] | |
do | |
TARGET_FILE=`readlink $TARGET_FILE` | |
cd `dirname $TARGET_FILE` | |
TARGET_FILE=`basename $TARGET_FILE` | |
done | |
# compute the canonicalized name by finding the physical path | |
BASE=`pwd -P` | |
# cd back to the original working directory | |
cd $CWD |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment