From 968a1bd80fd80e5df3dc62bbc9a93851fe878b92 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 19 Sep 2019 18:43:16 +0300 Subject: [PATCH] docs: update --- README.md | 3 ++- manuscript/chapter2.txt | 4 ++++ manuscript/chapter5.txt | 3 ++- test.sh | 15 +++++++++++++++ 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index eb70f47..54109eb 100644 --- a/README.md +++ b/README.md @@ -1070,7 +1070,8 @@ Alternative to the `dirname` command. ```sh dirname() { # Usage: dirname "path" - dir=${1%%/} + dir=${1:-.} + dir=${dir%%${dir##*[!/]}} [[ "${dir##*/*}" ]] && dir=. dir=${dir%/*} diff --git a/manuscript/chapter2.txt b/manuscript/chapter2.txt index 08c3b22..db04bbf 100644 --- a/manuscript/chapter2.txt +++ b/manuscript/chapter2.txt @@ -5,6 +5,8 @@ Enabling `extdebug` allows access to the `BASH_ARGV` array which stores the current function’s arguments in reverse. +**CAVEAT**: Requires `shopt -s compat44` in `bash` 5.0+. + **Example Function:** ```sh @@ -41,6 +43,8 @@ allows us to effectively remove array duplicates. **CAVEAT:** Requires `bash` 4+ +**CAVEAT:** List order may not stay the same. + **Example Function:** ```sh diff --git a/manuscript/chapter5.txt b/manuscript/chapter5.txt index e602a67..c3cd5e5 100644 --- a/manuscript/chapter5.txt +++ b/manuscript/chapter5.txt @@ -9,7 +9,8 @@ Alternative to the `dirname` command. ```sh dirname() { # Usage: dirname "path" - dir=${1%%/} + dir=${1:-.} + dir=${dir%%${dir##*[!/]}} [[ "${dir##*/*}" ]] && dir=. dir=${dir%/*} diff --git a/test.sh b/test.sh index 57205b2..b859a38 100755 --- a/test.sh +++ b/test.sh @@ -129,6 +129,21 @@ test_dirname() { result="$(dirname "something/")" 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() {