bible: better dirname function

This commit is contained in:
Dylan Araps 2019-09-22 14:25:57 +03:00
parent 70dbe8feba
commit 4d14f5446f
2 changed files with 30 additions and 12 deletions

View File

@ -1070,15 +1070,24 @@ Alternative to the `dirname` command.
```sh
dirname() {
# Usage: dirname "path"
dir=${1:-.}
dir=${dir%%${dir##*[!/]}}
local tmp=${1:-.}
[[ "${dir##*/*}" ]] && dir=.
[[ $tmp != *[!/]* ]] && {
printf '/\n'
return
}
dir=${dir%/*}
dir=${dir%%${dir##*[!/]}}
tmp=${tmp%%"${tmp##*[!/]}"}
printf '%s\n' "${dir:-/}"
[[ $tmp != */* ]] && {
printf '.\n'
return
}
tmp=${tmp%/*}
tmp=${tmp%%"${tmp##*[!/]}"}
printf '%s\n' "${tmp:-/}"
}
```

View File

@ -9,15 +9,24 @@ Alternative to the `dirname` command.
```sh
dirname() {
# Usage: dirname "path"
dir=${1:-.}
dir=${dir%%${dir##*[!/]}}
local tmp=${1:-.}
[[ "${dir##*/*}" ]] && dir=.
[[ $tmp != *[!/]* ]] && {
printf '/\n'
return
}
dir=${dir%/*}
dir=${dir%%${dir##*[!/]}}
tmp=${tmp%%"${tmp##*[!/]}"}
printf '%s\n' "${dir:-/}"
[[ $tmp != */* ]] && {
printf '.\n'
return
}
tmp=${tmp%/*}
tmp=${tmp%%"${tmp##*[!/]}"}
printf '%s\n' "${tmp:-/}"
}
```