Last active
August 29, 2015 14:05
-
-
Save meoow/0c33aa47ecead69b0b42 to your computer and use it in GitHub Desktop.
return absolute path in pure bash
This file contains 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
#get absolute path(shell) | |
abspath(){ #{{{ | |
local thePath | |
if [[ ! "$1" =~ ^/ ]];then | |
thePath="$PWD/$1" | |
else | |
thePath="$1" | |
fi | |
IFS=/ parr=($thePath) | |
declare -a outp | |
for i in "${parr[@]}";do | |
case "$i" in | |
''|.) continue ;; | |
..) | |
len=${#outp[@]} | |
if ((len==0));then | |
continue | |
else | |
unset outp[len-1] | |
fi | |
;; | |
*) | |
len=${#outp[@]} | |
outp[len]=$i | |
;; | |
esac | |
done | |
echo /"${outp[*]}" | |
} #}}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment