bible: fix dirname. Closes #76

This commit is contained in:
Dylan Araps
2019-09-19 16:23:20 +03:00
parent 6508ca0c9d
commit ea8fd2369c
4 changed files with 17 additions and 9 deletions

View File

@@ -1061,7 +1061,11 @@ Alternative to the `dirname` command.
```sh ```sh
dirname() { dirname() {
# Usage: dirname "path" # Usage: dirname "path"
printf '%s\n' "${1%/*}/" dir=${1%%/}
[[ $dir == */* ]] || dir=.
printf '%s\n' "${dir%/*}"
} }
``` ```
@@ -1069,10 +1073,10 @@ dirname() {
```shell ```shell
$ dirname ~/Pictures/Wallpapers/1.jpg $ dirname ~/Pictures/Wallpapers/1.jpg
/home/black/Pictures/Wallpapers/ /home/black/Pictures/Wallpapers
$ dirname ~/Pictures/Downloads/ $ dirname ~/Pictures/Downloads/
/home/black/Pictures/ /home/black/Pictures
``` ```
## Get the base-name of a file path ## Get the base-name of a file path

View File

@@ -10,8 +10,8 @@ Surprisingly, `sleep` is an external command and not a `bash` built-in.
```sh ```sh
read_sleep() { read_sleep() {
# Usage: sleep 1 # Usage: read_sleep 1
# sleep 0.2 # read_sleep 0.2
read -rt "$1" <> <(:) || : read -rt "$1" <> <(:) || :
} }
``` ```

View File

@@ -9,7 +9,11 @@ Alternative to the `dirname` command.
```sh ```sh
dirname() { dirname() {
# Usage: dirname "path" # Usage: dirname "path"
printf '%s\n' "${1%/*}/" dir=${1%%/}
[[ $dir == */* ]] || dir=.
printf '%s\n' "${dir%/*}"
} }
``` ```
@@ -17,10 +21,10 @@ dirname() {
```shell ```shell
$ dirname ~/Pictures/Wallpapers/1.jpg $ dirname ~/Pictures/Wallpapers/1.jpg
/home/black/Pictures/Wallpapers/ /home/black/Pictures/Wallpapers
$ dirname ~/Pictures/Downloads/ $ dirname ~/Pictures/Downloads/
/home/black/Pictures/ /home/black/Pictures
``` ```
## Get the base-name of a file path ## Get the base-name of a file path

View File

@@ -116,7 +116,7 @@ test_count() {
test_dirname() { test_dirname() {
result="$(dirname "/home/black/Pictures/Wallpapers/1.jpg")" result="$(dirname "/home/black/Pictures/Wallpapers/1.jpg")"
assert_equals "$result" "/home/black/Pictures/Wallpapers/" assert_equals "$result" "/home/black/Pictures/Wallpapers"
} }
test_basename() { test_basename() {