diff options
author | Kerin Millar <kfm@plushkava.net> | 2024-06-27 21:36:10 +0100 |
---|---|---|
committer | Kerin Millar <kfm@plushkava.net> | 2024-06-28 18:39:27 +0100 |
commit | 9dc4a6c4a383b1214babe14b4b7091ad56840486 (patch) | |
tree | 39da3c9afc756dade1f05717ba45aa6189a1ac81 | |
parent | Describe the SENTINEL variable (diff) | |
download | gentoo-functions-9dc4a6c4a383b1214babe14b4b7091ad56840486.tar.gz gentoo-functions-9dc4a6c4a383b1214babe14b4b7091ad56840486.tar.bz2 gentoo-functions-9dc4a6c4a383b1214babe14b4b7091ad56840486.zip |
Add the up() function to experimental
As based on the implementation in Maarten Billemont's bashlib library.
Signed-off-by: Kerin Millar <kfm@plushkava.net>
-rw-r--r-- | functions/experimental.sh | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/functions/experimental.sh b/functions/experimental.sh index f577fa7..e02923a 100644 --- a/functions/experimental.sh +++ b/functions/experimental.sh @@ -51,3 +51,31 @@ prepend_ts() prepend_ts } + +# +# Takes the first parameter as either a relative pathname or an integer +# referring to a number of iterations. To be recognised as a pathname, the first +# four characters must form the special prefix, ".../". It recurses upwards from +# the current directory until either the relative pathname is found to exist, +# the specified number of iterations has occurred, or the root directory is +# encountered. In the event that the root directory is reached without either of +# the first two conditions being satisfied, the return value shall be 1. +# Otherwise, the value of PWD shall be printed to the standard output. +# +up() +{ + local i + + i=0 + while [ "${PWD}" != / ]; do + chdir ../ + case $1 in + .../*) + test -e "${1#.../}" + ;; + *) + test "$(( i += 1 ))" -eq "$1" + esac \ + && pwd && return + done +} |