mirror of
https://github.com/dylanaraps/pure-bash-bible.git
synced 2025-08-28 17:49:55 +02:00
docs: update
This commit is contained in:
@@ -1070,7 +1070,8 @@ Alternative to the `dirname` command.
|
|||||||
```sh
|
```sh
|
||||||
dirname() {
|
dirname() {
|
||||||
# Usage: dirname "path"
|
# Usage: dirname "path"
|
||||||
dir=${1%%/}
|
dir=${1:-.}
|
||||||
|
dir=${dir%%${dir##*[!/]}}
|
||||||
[[ "${dir##*/*}" ]] && dir=.
|
[[ "${dir##*/*}" ]] && dir=.
|
||||||
dir=${dir%/*}
|
dir=${dir%/*}
|
||||||
|
|
||||||
|
@@ -5,6 +5,8 @@
|
|||||||
Enabling `extdebug` allows access to the `BASH_ARGV` array which stores
|
Enabling `extdebug` allows access to the `BASH_ARGV` array which stores
|
||||||
the current function’s arguments in reverse.
|
the current function’s arguments in reverse.
|
||||||
|
|
||||||
|
**CAVEAT**: Requires `shopt -s compat44` in `bash` 5.0+.
|
||||||
|
|
||||||
**Example Function:**
|
**Example Function:**
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
@@ -41,6 +43,8 @@ allows us to effectively remove array duplicates.
|
|||||||
|
|
||||||
**CAVEAT:** Requires `bash` 4+
|
**CAVEAT:** Requires `bash` 4+
|
||||||
|
|
||||||
|
**CAVEAT:** List order may not stay the same.
|
||||||
|
|
||||||
**Example Function:**
|
**Example Function:**
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
|
@@ -9,7 +9,8 @@ Alternative to the `dirname` command.
|
|||||||
```sh
|
```sh
|
||||||
dirname() {
|
dirname() {
|
||||||
# Usage: dirname "path"
|
# Usage: dirname "path"
|
||||||
dir=${1%%/}
|
dir=${1:-.}
|
||||||
|
dir=${dir%%${dir##*[!/]}}
|
||||||
[[ "${dir##*/*}" ]] && dir=.
|
[[ "${dir##*/*}" ]] && dir=.
|
||||||
dir=${dir%/*}
|
dir=${dir%/*}
|
||||||
|
|
||||||
|
15
test.sh
15
test.sh
@@ -129,6 +129,21 @@ test_dirname() {
|
|||||||
|
|
||||||
result="$(dirname "something/")"
|
result="$(dirname "something/")"
|
||||||
assert_equals "$result" "."
|
assert_equals "$result" "."
|
||||||
|
|
||||||
|
result="$(dirname "//")"
|
||||||
|
assert_equals "$result" "/"
|
||||||
|
|
||||||
|
result="$(dirname "//foo")"
|
||||||
|
assert_equals "$result" "/"
|
||||||
|
|
||||||
|
result="$(dirname "")"
|
||||||
|
assert_equals "$result" "."
|
||||||
|
|
||||||
|
result="$(dirname "something//")"
|
||||||
|
assert_equals "$result" "."
|
||||||
|
|
||||||
|
result="$(dirname "something/////////////////////")"
|
||||||
|
assert_equals "$result" "."
|
||||||
}
|
}
|
||||||
|
|
||||||
test_basename() {
|
test_basename() {
|
||||||
|
Reference in New Issue
Block a user