mirror of
https://github.com/jimeh/dotify.git
synced 2026-02-19 10:06:39 +00:00
Add absolute path helpers
This commit is contained in:
@@ -31,6 +31,7 @@ shopt -s extglob
|
|||||||
# Helper functions
|
# Helper functions
|
||||||
#
|
#
|
||||||
|
|
||||||
|
source "../lib/helpers/absolute-paths.sh"
|
||||||
source "../lib/helpers/arguments.sh"
|
source "../lib/helpers/arguments.sh"
|
||||||
source "../lib/helpers/trim.sh"
|
source "../lib/helpers/trim.sh"
|
||||||
|
|
||||||
|
|||||||
30
src/lib/helpers/absolute-paths.sh
Normal file
30
src/lib/helpers/absolute-paths.sh
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
# Get absolute path to directory given file resides in.
|
||||||
|
#
|
||||||
|
# Example:
|
||||||
|
#
|
||||||
|
# $ cd /tmp/foo-bar
|
||||||
|
# $ abs_dirname hello/world.txt
|
||||||
|
# /tmp/foo-bar/hello/world.txt
|
||||||
|
#
|
||||||
|
abs_dirname() {
|
||||||
|
local cwd="$(pwd)"
|
||||||
|
local path="$1"
|
||||||
|
|
||||||
|
while [ -n "$path" ]; do
|
||||||
|
cd "${path%/*}"
|
||||||
|
local name="${path##*/}"
|
||||||
|
path="$(resolve_link "$name" || true)"
|
||||||
|
done
|
||||||
|
|
||||||
|
pwd
|
||||||
|
cd "$cwd"
|
||||||
|
}
|
||||||
|
|
||||||
|
abs_path() {
|
||||||
|
local path="$1"
|
||||||
|
echo "$(cd "$(abs_dirname "$path")" && pwd)/$(basename "$path")"
|
||||||
|
}
|
||||||
|
|
||||||
|
resolve_link() {
|
||||||
|
$(type -p greadlink readlink | head -1) $1
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user