Last active
August 29, 2015 14:05
-
-
Save x100ex/2ed7dc3414ee42ea883a to your computer and use it in GitHub Desktop.
pure bash abspath
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 | |
abspath() { | |
local thePath | |
if [[ ! "$1" =~ ^/ ]] | |
then | |
thePath="$PWD/$1" | |
else | |
thePath="$1" | |
fi | |
echo "$thePath"|( | |
IFS=/ | |
read -a parr | |
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