mirror of
https://github.com/dylanaraps/pure-bash-bible.git
synced 2025-01-17 04:08:17 +01:00
bible: better dirname function
This commit is contained in:
parent
70dbe8feba
commit
4d14f5446f
21
README.md
21
README.md
@ -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:-/}"
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -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:-/}"
|
||||
}
|
||||
```
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user