-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathshlib_relToAbsPath
More file actions
39 lines (34 loc) · 1.14 KB
/
shlib_relToAbsPath
File metadata and controls
39 lines (34 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# vim:et:ft=sh:sts=2:sw=2
#
# Copyright 2017 Kate Ward. All Rights Reserved.
# Released under the Apache 2.0 license.
#
# Author: kate.ward@forestent.com (Kate Ward)
# Repository: https://github.com/kward/shlib
SHLIB_PWD='pwd'
# Convert a relative path into it's absolute equivalent.
#
# This function will automatically prepend the current working directory if the
# path is not already absolute. It then removes all parent references ('../') to
# reconstruct the proper absolute path.
#
# Args:
# shlib_path_: string: relative path
# Outputs:
# string: absolute path
shlib_relToAbsPath() {
shlib_path_=$1
# Prepend current directory to relative paths.
echo "${shlib_path_}" |grep '^/' >/dev/null 2>&1 \
|| shlib_path_="$(${SHLIB_PWD})/${shlib_path_}"
# Clean up the path. If all `sed` commands supported true regular expressions,
# then this is what they would do.
shlib_old_=${shlib_path_}
while true; do
shlib_new_=$(echo "${shlib_old_}" |sed 's/[^/]*\/\.\.\/*//g;s/\/\.\//\//')
[ "${shlib_old_}" = "${shlib_new_}" ] && break
shlib_old_=${shlib_new_}
done
echo "${shlib_new_}"
unset shlib_old_ shlib_new_ shlib_path_
}