From 49bd538c26785deff880703ad953d0d05cf13392 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 19 Sep 2019 17:06:26 +0300 Subject: [PATCH] bible: really fix dirname --- README.md | 6 +++--- manuscript/chapter5.txt | 6 +++--- test.sh | 14 +++++++++++++- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index a81bcd1..c22b3f5 100644 --- a/README.md +++ b/README.md @@ -1067,11 +1067,11 @@ Alternative to the `dirname` command. dirname() { # Usage: dirname "path" dir=${1%%/} + dir=${dir%/*} - [[ $dir ]] || dir=// - [[ $dir == */* ]] || dir=. + [[ $1 == */* ]] || dir=. - printf '%s\n' "${dir%/*}" + printf '%s\n' "${dir:-/}" } ``` diff --git a/manuscript/chapter5.txt b/manuscript/chapter5.txt index 5534433..84b9e2b 100644 --- a/manuscript/chapter5.txt +++ b/manuscript/chapter5.txt @@ -10,11 +10,11 @@ Alternative to the `dirname` command. dirname() { # Usage: dirname "path" dir=${1%%/} + dir=${dir%/*} - [[ $dir ]] || dir=// - [[ $dir == */* ]] || dir=. + [[ $1 == */* ]] || dir=. - printf '%s\n' "${dir%/*}" + printf '%s\n' "${dir:-/}" } ``` diff --git a/test.sh b/test.sh index 8be55c8..debf2da 100755 --- a/test.sh +++ b/test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -# shellcheck source=/dev/null +# shellcheck source=/dev/null disable=2178,2128 # # Tests for the Pure Bash Bible. @@ -117,6 +117,18 @@ test_count() { test_dirname() { result="$(dirname "/home/black/Pictures/Wallpapers/1.jpg")" assert_equals "$result" "/home/black/Pictures/Wallpapers" + + result="$(dirname "/")" + assert_equals "$result" "/" + + result="$(dirname "/foo")" + assert_equals "$result" "/" + + result="$(dirname ".")" + assert_equals "$result" "." + + result="$(dirname "/foo/foo")" + assert_equals "$result" "/foo" } test_basename() {