mirror of
https://github.com/dylanaraps/pure-sh-bible
synced 2024-05-03 22:23:13 +02:00
docs: update
This commit is contained in:
parent
303a209161
commit
dc6de0ef34
43
README.md
43
README.md
|
@ -527,10 +527,34 @@ Alternative to the `dirname` command.
|
|||
```sh
|
||||
dirname() {
|
||||
# Usage: dirname "path"
|
||||
dir=${1%%/}
|
||||
|
||||
# If '$1' is empty set 'dir' to '.', else '$1'.
|
||||
dir=${1:-.}
|
||||
|
||||
# Strip all trailing forward-slashes '/' from
|
||||
# the end of the string.
|
||||
#
|
||||
# "${dir##*[!/]}": Remove all non-forward-slashes
|
||||
# from the start of the string, leaving us with only
|
||||
# the trailing slashes.
|
||||
# "${dir%%"${}"}: Remove the result of the above
|
||||
# substitution (a string of forward slashes) from the
|
||||
# end of the original string.
|
||||
dir=${dir%%"${dir##*[!/]}"}
|
||||
|
||||
# If the variable *does not* contain any forward slashes
|
||||
# set its value to '.'.
|
||||
[ "${dir##*/*}" ] && dir=.
|
||||
|
||||
# Remove everything *after* the last forward-slash '/'.
|
||||
dir=${dir%/*}
|
||||
|
||||
# Again, strip all trailing forward-slashes '/' from
|
||||
# the end of the string (see above).
|
||||
dir=${dir%%"${dir##*[!/]}"}
|
||||
|
||||
# Print the resulting string and if it is empty,
|
||||
# print '/'.
|
||||
printf '%s\n' "${dir:-/}"
|
||||
}
|
||||
```
|
||||
|
@ -554,10 +578,27 @@ Alternative to the `basename` command.
|
|||
```sh
|
||||
basename() {
|
||||
# Usage: basename "path" ["suffix"]
|
||||
|
||||
# Strip all trailing forward-slashes '/' from
|
||||
# the end of the string.
|
||||
#
|
||||
# "${1##*[!/]}": Remove all non-forward-slashes
|
||||
# from the start of the string, leaving us with only
|
||||
# the trailing slashes.
|
||||
# "${1%%"${}"}: Remove the result of the above
|
||||
# substitution (a string of forward slashes) from the
|
||||
# end of the original string.
|
||||
dir=${1%${1##*[!/]}}
|
||||
|
||||
# Remove everything before the final forward-slash '/'.
|
||||
dir=${dir##*/}
|
||||
|
||||
# If a suffix was passed to the function, remove it from
|
||||
# the end of the resulting string.
|
||||
dir=${dir%"$2"}
|
||||
|
||||
# Print the resulting string and if it is empty,
|
||||
# print '/'.
|
||||
printf '%s\n' "${dir:-/}"
|
||||
}
|
||||
```
|
||||
|
|
Loading…
Reference in a new issue