From ea8fd2369c8460ffd6ed98d569d77607ab2735c4 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 19 Sep 2019 16:23:20 +0300 Subject: [PATCH] bible: fix dirname. Closes #76 --- README.md | 10 +++++++--- manuscript/chapter19.txt | 4 ++-- manuscript/chapter5.txt | 10 +++++++--- test.sh | 2 +- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 7e4f9c0..5b33368 100644 --- a/README.md +++ b/README.md @@ -1061,7 +1061,11 @@ Alternative to the `dirname` command. ```sh dirname() { # Usage: dirname "path" - printf '%s\n' "${1%/*}/" + dir=${1%%/} + + [[ $dir == */* ]] || dir=. + + printf '%s\n' "${dir%/*}" } ``` @@ -1069,10 +1073,10 @@ dirname() { ```shell $ dirname ~/Pictures/Wallpapers/1.jpg -/home/black/Pictures/Wallpapers/ +/home/black/Pictures/Wallpapers $ dirname ~/Pictures/Downloads/ -/home/black/Pictures/ +/home/black/Pictures ``` ## Get the base-name of a file path diff --git a/manuscript/chapter19.txt b/manuscript/chapter19.txt index c7bc026..62aefef 100644 --- a/manuscript/chapter19.txt +++ b/manuscript/chapter19.txt @@ -10,8 +10,8 @@ Surprisingly, `sleep` is an external command and not a `bash` built-in. ```sh read_sleep() { - # Usage: sleep 1 - # sleep 0.2 + # Usage: read_sleep 1 + # read_sleep 0.2 read -rt "$1" <> <(:) || : } ``` diff --git a/manuscript/chapter5.txt b/manuscript/chapter5.txt index 8725e1e..7bf7554 100644 --- a/manuscript/chapter5.txt +++ b/manuscript/chapter5.txt @@ -9,7 +9,11 @@ Alternative to the `dirname` command. ```sh dirname() { # Usage: dirname "path" - printf '%s\n' "${1%/*}/" + dir=${1%%/} + + [[ $dir == */* ]] || dir=. + + printf '%s\n' "${dir%/*}" } ``` @@ -17,10 +21,10 @@ dirname() { ```shell $ dirname ~/Pictures/Wallpapers/1.jpg -/home/black/Pictures/Wallpapers/ +/home/black/Pictures/Wallpapers $ dirname ~/Pictures/Downloads/ -/home/black/Pictures/ +/home/black/Pictures ``` ## Get the base-name of a file path diff --git a/test.sh b/test.sh index 94da32e..8be55c8 100755 --- a/test.sh +++ b/test.sh @@ -116,7 +116,7 @@ test_count() { test_dirname() { result="$(dirname "/home/black/Pictures/Wallpapers/1.jpg")" - assert_equals "$result" "/home/black/Pictures/Wallpapers/" + assert_equals "$result" "/home/black/Pictures/Wallpapers" } test_basename() {