shell-base/modules/scripting/path.sh

42 lines
1.0 KiB
Bash
Raw Normal View History

#!/bin/bash
isAbsPath() {
if [[ "${1:0:1}" == / || "${1:0:2}" == ~[/a-z] ]]
then
echo "true"
return 0
else
return 1
fi
}
# get full absolute path from a current or parent path
function abs_path {
local target="$1"
[[ $(isAbsPath $target) ]] && { echo $target; return 0; }
# echo relative path finding absolute
if [[ ! $(echo "$target" | sed 's/^\.\..*//') ]]; then
# echo .. parent directory
echo "$(dirname "$(pwd)")/${target/..//}" | tr -s /
return 0
fi
if [[ ! $(echo "$target" | sed 's/^\..*//') ]]; then
# echo . current directory
echo "$(pwd)/${target/.//}" | tr -s /
return 0
fi
# echo simple relative
echo "$(pwd)/$target" | tr -s /
}
# get full absolute path from a current or parent path
function abs_dir {
# with no trailing / will assume that is a file
# and remove it so return only the directory.
local target="$1"
[[ ! "$target" == */ ]] && target=$(dirname $target)
echo $(abs_path $target)
}