commit:     9dc4a6c4a383b1214babe14b4b7091ad56840486
Author:     Kerin Millar <kfm <AT> plushkava <DOT> net>
AuthorDate: Thu Jun 27 20:36:10 2024 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Fri Jun 28 17:39:27 2024 +0000
URL:        
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=9dc4a6c4

Add the up() function to experimental

As based on the implementation in Maarten Billemont's bashlib library.

Signed-off-by: Kerin Millar <kfm <AT> plushkava.net>

 functions/experimental.sh | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

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
+}

Reply via email to