mirror of
https://github.com/dylanaraps/pure-bash-bible.git
synced 2025-09-03 04:12:36 +02:00
bible: fix dirname. Closes #76
This commit is contained in:
10
README.md
10
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
|
||||
|
@@ -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" <> <(:) || :
|
||||
}
|
||||
```
|
||||
|
@@ -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
|
||||
|
Reference in New Issue
Block a user