summaryrefslogtreecommitdiffstats
path: root/pkgs/shell/deref
diff options
context:
space:
mode:
Diffstat (limited to 'pkgs/shell/deref')
-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