summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2024-05-08 17:05:54 +0200
committertv <tv@krebsco.de>2024-05-08 17:05:54 +0200
commit7a2e587171482f39353efd80a159dce162a67eb1 (patch)
treec4bf9bd1b8fdf84de19c33409681f8f6c370daf4
parentf5cf6c74f046647240a3a7070c4e6cf28d24e849 (diff)
deref: init
-rwxr-xr-xpkgs/shell/deref89
1 files changed, 89 insertions, 0 deletions
diff --git a/pkgs/shell/deref b/pkgs/shell/deref
new file mode 100755
index 0000000..05da4ba
--- /dev/null
+++ b/pkgs/shell/deref
@@ -0,0 +1,89 @@
+#! /bin/sh
+#!buildShellBin prepend-path=coreutils:which
+#
+# NAME
+# deref - dereference a name or path, showing all intermediaries
+#
+# SYNOPSIS
+# deref [OPTION]... NAME...
+#
+# DESCRIPTION
+#
+#
+# --show=asis, --show=as-is
+# When encountering relative file names, show them as-is.
+# This is the default behavior.
+#
+# --show=all
+# When encountering relative file names, show them as-is
+# and additionally show their absolute file name.
+#
+# --show=abs, --show=absolute
+# When encountering relative file names, show their absolute
+# file name.
+#
+
+set -efu
+
+showname=asis
+case $1 in
+ (--show=asis|--show=as-is|--show=all|--show=abs|--show=absolute)
+ showname=${1#--show=}
+ shift
+ ;;
+ (-*)
+ echo "$0: bad argument: $1" >&2
+ exit 1
+ ;;
+esac
+
+showname() {
+ if test -L "$1" || test -e "$1"; then
+ case $1 in
+ /*)
+ ls -d --color "$1"
+ ;;
+ *)
+ case $showname in
+ asis|as-is)
+ ls -d --color "$1"
+ ;;
+ abs|absolute)
+ ls -d --color "$(realpath -sm "$1")"
+ ;;
+ all)
+ echo "$(ls -d --color "$1")=$(ls -d --color "$(realpath -sm "$1")")"
+ ;;
+ esac
+ ;;
+ esac
+ else
+ printf '%s' "$name"
+ fi
+}
+
+pwd=$PWD
+for name; do
+ cd "$pwd"
+ path=
+ sep=
+
+ if ! test -L "$name" && ! test -e "$name"; then
+ if which=$(which "$name" 2>/dev/null); then
+ path=$name
+ sep=' ⇒ '
+ name=$which
+ fi
+ fi
+
+ while test -L "$name"; do
+ cd "$(dirname "$name")"
+ path=${path+$path$sep}$(showname "$name")
+ sep=' → '
+ name=$(readlink "$name")
+ done
+
+ path=${path+$path$sep}$(showname "$name")
+
+ echo "$path"
+done