ajouter quelques fonctions de base.path.sh

This commit is contained in:
Jephté Clain 2024-05-12 14:15:37 +04:00
parent cbe26be1dc
commit f42dcb978a
1 changed files with 69 additions and 1 deletions

View File

@ -185,7 +185,7 @@ function normpath() {
ap="$ap$part"
fi
done
recho "$ap"
echo "$ap"
}
function __normpath() {
# normaliser dans les cas simple le chemin absolu $1. sinon retourner 1.
@ -231,3 +231,71 @@ function abspath() {
# dans les cas spéciaux, il faut calculer "manuellement" le répertoire absolu
normpath "$ap"
}
function ppath() {
# Dans un chemin *absolu*, remplacer "$HOME" par "~" et "$(pwd)/" par "", afin
# que le chemin soit plus facile à lire. Le répertoire courant est spécifié par
# $2 ou $(pwd) si $2 est vide
local path="$1" cwd="$2"
path="$(abspath "$path")" # essayer de normaliser le chemin
[ -n "$cwd" ] || cwd="$(pwd)"
[ "$path" == "$cwd" ] && path="."
[ "$cwd" != "/" -a "$cwd" != "$HOME" ] && path="${path#$cwd/}"
[ "${path#$HOME/}" != "$path" ] && path="~${path#$HOME}"
echo "$path"
}
function ppath2() {
# Comme ppath() mais afficher '.' comme '../$dirname'
local path="$1" cwd="$2"
path="$(abspath "$path")" # essayer de normaliser le chemin
[ -n "$cwd" ] || cwd="$(pwd)"
[ "$path" == "$cwd" ] && path="../$(basename -- "$path")"
[ "$cwd" != "/" -a "$cwd" != "$HOME" ] && path="${path#$cwd/}"
[ "${path#$HOME/}" != "$path" ] && path="~${path#$HOME}"
echo "$path"
}
function relpath() {
# Afficher le chemin relatif de $1 par rapport à $2. Si $2 n'est pas spécifié,
# on prend le répertoire courant. Si $1 ou $2 ne sont pas des chemins absolus,
# il sont transformés en chemins absolus par rapport à $3. Si $1==$2, retourner
# une chaine vide
local p="$(abspath "$1" "$3")" cwd="$2"
if [ -z "$cwd" ]; then
cwd="$(pwd)"
else
cwd="$(abspath "$cwd" "$3")"
fi
if [ "$p" == "$cwd" ]; then
echo ""
elif [ "${p#$cwd/}" != "$p" ]; then
echo "${p#$cwd/}"
else
local rp
while [ -n "$cwd" -a "${p#$cwd/}" == "$p" ]; do
rp="${rp:+$rp/}.."
cwd="${cwd%/*}"
done
rp="$rp/${p#$cwd/}"
# ${rp%//} traite le cas $1==/
echo "${rp%//}"
fi
}
function relpathx() {
# Comme relpath, mais pour un chemin vers un exécutable qu'il faut lancer:
# s'assurer qu'il y a une spécification de chemin, e.g. ./script
local p="$(relpath "$@")"
if [ -z "$p" ]; then
echo .
elif [ "${p#../}" != "$p" -o "${p#./}" != "$p" ]; then
echo "$p"
else
echo "./$p"
fi
}