#get absolute path(shell)
abspath(){#{{{local thePath
if[[ ! "$1"=~ ^/ ]];thenthePath="$PWD/$1"elsethePath="$1"fiIFS=/ parr=($thePath)declare -a outp
for i in "${parr[@]}";docase"$i" in
''|.)continue;;
..)len=${#outp[@]}if((len==0));thencontinueelseunset outp[len-1]fi;;
*)len=${#outp[@]}
outp[len]=$i;;esacdoneecho /"${outp[*]}"}#}}}
using gawk:
#get absolute path(gawk)
abspath_gawk(){#{{{if[[ -n "$1"]];thenecho$1|gawk '{ if(substr($0,1,1) != "/"){ path = ENVIRON["PWD"]"/"$0 } else path = $0 split(path, a, "/") n = asorti(a, b,"@ind_num_asc") for(i in a){ if(a[i]=="" || a[i]=="."){ delete a[i] } } n = asorti(a, b, "@ind_num_asc") m = 0 while(m!=n){ m = n for(i=1;i<=n;i++){ if(a[b[i]]==".."){ if(b[i-1] in a){ delete a[b[i-1]] delete a[b[i]] n = asorti(a, b, "@ind_num_asc") break } else exit 1 } } } n = asorti(a, b, "@ind_num_asc") if(n==0){ printf "/" } else { for(i=1;i<=n;i++){ printf "/"a[b[i]] } } }'fi}