From 11f162c91a6755589be2f68b5fdc2facdc281f30 Mon Sep 17 00:00:00 2001 From: Mark Hoekveen Date: Sat, 8 Aug 2026 18:06:50 +0200 Subject: [PATCH] ln wrapper --- README.md | 1 + bin/lnw | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100755 bin/lnw diff --git a/README.md b/README.md index f66fb48..eec0a48 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ While created for my own use, they are shared here in case others find them usef - **check**: Validates script dependencies and configuration - **e-ink**: Display handler for e-ink displays with image cycling support - **indeel**: File organization tool that watches a directory and sorts incoming files based on content hashes +- **lnw**: `ln` wrapper that always creates a symbolic link, auto-detects which argument is the existing target, and converts paths to absolute - **mover**: Monitors a source directory and moves files with specified extensions to a destination directory - **pushover**: Sends push notifications via Pushover API - **stats**: HTTP server that provides system statistics (CPU, memory, disk usage, etc.) in JSON format diff --git a/bin/lnw b/bin/lnw new file mode 100755 index 0000000..ed6dd8a --- /dev/null +++ b/bin/lnw @@ -0,0 +1,50 @@ +#!/bin/bash +# ln? pfft +# Ja het was waarschijnlijk minder moeite geweest om het te onthouden +# dan om dit scriptje te schrijven. + +set -euo pipefail + +usage() { + echo "Ook altijd geen idee hoe ln ook alweer werkt? Geen zorgen." + echo "Doe: $0 " >&2 + echo " of: $0 " >&2 + exit 1 +} + +if [ $# -ne 2 ]; then + usage +fi + +a="$1" +b="$2" + +a_exists=0 +b_exists=0 +if [ -e "$a" ]; then a_exists=1; fi +if [ -e "$b" ]; then b_exists=1; fi + +if [ "$a_exists" -eq 1 ] && [ "$b_exists" -eq 1 ]; then + # is één van de twee al een link? Dan is het goed. + if { [ -L "$a" ] && [ "$(readlink "$a")" = "$(realpath "$b")" ]; } || \ + { [ -L "$b" ] && [ "$(readlink "$b")" = "$(realpath "$a")" ]; }; then + exit 0 + fi + echo "[FOUT] deze bestaan al: $a, $b" >&2 + exit 1 +# Geen van beide bestaat +elif [ "$a_exists" -eq 0 ] && [ "$b_exists" -eq 0 ]; then + echo "[FOUT] allebei niet gevonden: $a, $b" >&2 + exit 1 +elif [ "$a_exists" -eq 1 ]; then + target="$a" + link="$b" +else + target="$b" + link="$a" +fi + +target_abs="$(realpath "$target")" +link_abs="$(realpath -m "$link")" + +ln -s "$target_abs" "$link_abs"