LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Blogs > konsolebox
User Name
Password

Notices


Rate this Entry

Getting Absolute Paths of UNIX Directories and Filenames in Shell Scripts

Posted 07-08-2011 at 03:05 AM by konsolebox
Updated 05-27-2018 at 03:20 PM by konsolebox
Tags bash, ksh, script, shell, zsh

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
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
}
Ksh93
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
}
PDKsh or MKsh
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
}
Zsh
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
}
Any other shell based on bourne shell. This requires 'pwd'.
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
}
Check other generic solutions in https://github.com/konsolebox/script...ter/getabspath.

getabspath.c - See http://www.linuxquestions.org/questi...ml#post3648275.
Posted in Howtos
Views 2905 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 01:51 PM.

Main Menu
Advertisement
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration