From 7a2e587171482f39353efd80a159dce162a67eb1 Mon Sep 17 00:00:00 2001 From: tv Date: Wed, 8 May 2024 17:05:54 +0200 Subject: deref: init --- pkgs/shell/deref | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100755 pkgs/shell/deref (limited to 'pkgs') 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 -- cgit v1.2.3