From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id BADF81581C1 for ; Sun, 7 Jul 2024 05:55:43 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 33F98E2A64; Sun, 7 Jul 2024 05:55:42 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [IPv6:2001:470:ea4a:1:5054:ff:fec7:86e4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 1C39DE2A64 for ; Sun, 7 Jul 2024 05:55:42 +0000 (UTC) Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 62B9F340C7F for ; Sun, 7 Jul 2024 05:55:41 +0000 (UTC) Received: from localhost.localdomain (localhost [IPv6:::1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id A007A1DDB for ; Sun, 7 Jul 2024 05:55:38 +0000 (UTC) From: "Sam James" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Sam James" Message-ID: <1719596367.9dc4a6c4a383b1214babe14b4b7091ad56840486.sam@gentoo> Subject: [gentoo-commits] proj/gentoo-functions:master commit in: functions/ X-VCS-Repository: proj/gentoo-functions X-VCS-Files: functions/experimental.sh X-VCS-Directories: functions/ X-VCS-Committer: sam X-VCS-Committer-Name: Sam James X-VCS-Revision: 9dc4a6c4a383b1214babe14b4b7091ad56840486 X-VCS-Branch: master Date: Sun, 7 Jul 2024 05:55:38 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply X-Archives-Salt: 619d394c-1844-43a7-821c-ad8a1194104e X-Archives-Hash: a452e621ffd786d2a7ea2f9f54b71bc9 commit: 9dc4a6c4a383b1214babe14b4b7091ad56840486 Author: Kerin Millar plushkava net> AuthorDate: Thu Jun 27 20:36:10 2024 +0000 Commit: Sam James gentoo 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 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 +}