Getting Absolute Paths of UNIX Directories and Filenames in Shell Scripts
This post is a compilation of the function getabspath() for different shells that can be used to get the absolute form of a path. The function accepts an argument path then returns the absolute form of it to the variable $__.
Bash
Ksh93
PDKsh or MKsh
Zsh
Any other shell based on bourne shell. This requires 'pwd'.
Check other generic solutions in https://github.com/konsolebox/script...ter/getabspath.
getabspath.c - See http://www.linuxquestions.org/questi...ml#post3648275.
Bash
Code:
function getabspath { local t=() i=0 IFS=/ case $1 in /*) __=${1#/} ;; *) __=${PWD#/}/$1 ;; esac case $- in *f*) set -- $__ ;; *) set -f set -- $__ set +f ;; esac for __; do case $__ in ..) (( i )) && unset 't[--i]' continue ;; .|'') continue ;; esac t[i++]=$__ done case $1 in */) (( i )) && __="/${t[*]}/" || __=/ ;; *) (( i )) && __="/${t[*]}" || __=/. ;; esac }
Code:
function getabspath { typeset t i=0 IFS=/ case $1 in /*) __=${1#/} ;; *) __=${PWD#/}/$1 ;; esac case $- in *f*) set -- $__ ;; *) set -f set -- $__ set +f ;; esac for __; do case $__ in ..) (( i )) && unset 't[--i]' continue ;; .|'') continue ;; esac t[i++]=$__ done case $1 in */) (( i )) && __="/${t[*]}/" || __=/ ;; *) (( i )) && __="/${t[*]}" || __=/. ;; esac }
Code:
getabspath() { typeset t i=0 IFS=/ case $1 in /*) __=${1#/} ;; *) __=${PWD#/}/$1 ;; esac case $- in *f*) set -- $__ ;; *) set -f set -- $__ set +f ;; esac for __; do case $__ in ..) (( i )) && unset 't[--i]' continue ;; .|'') continue ;; esac t[i++]=$__ done case $1 in */) (( i )) && __="/${t[*]}/" || __=/ ;; *) (( i )) && __="/${t[*]}" || __=/. ;; esac }
Code:
function getabspath { local t i=0 IFS=/ set -A t case $1 in /*) set -- ${=1#/} ;; *) set -- ${=PWD#/} ${=1} ;; esac for __; do case $__ in ..) (( i )) && t[i--]=() continue ;; .|'') continue ;; esac t[++i]=$__ done case $__ in */) (( i )) && __="/${t[*]}/" || __=/ ;; *) (( i )) && __="/${t[*]}" || __=/. ;; esac }
Code:
getabspath() { GETABSPATH_ARG=$1 GETABSPATH_OLD_FLAGS=$- GETABSPATH_OLD_IFS=$IFS IFS=/ set -f case $1 in /*) set -- $1 ;; *) set -- `exec pwd` $1 ;; esac __= while [ "$#" -gt 0 ]; do case $1 in ..|.|'') shift continue ;; esac GETABSPATH_TEMP=$1 shift while [ "$#" -gt 0 ]; do case $1 in .|'') shift continue ;; esac break done case $1 in ..) shift set -- $__ "$@" __= continue ;; esac __=$__/$GETABSPATH_TEMP done case $GETABSPATH_OLD_FLAGS in *f*) ;; *) set +f ;; esac IFS=$GETABSPATH_OLD_IFS case $GETABSPATH_ARG in */) __=$__/ ;; *) [ -z "$__" ] && __=/. ;; esac }
getabspath.c - See http://www.linuxquestions.org/questi...ml#post3648275.
Total Comments 0