#!/bin/bash # ============================================================================== # uuid-install.sh # # Installs the `uuid` shell alias system-wide. # # Usage: # curl -fsSL https://curl-pipe-bash.watzinger.biz/uuid-install.sh | sudo bash # # What it does: # - uuid: alias for `uuidgen` # - Installs uuidgen (uuid-runtime on Debian/Ubuntu, util-linux elsewhere), # which the alias depends on, via the detected system package manager # (apt/dnf/yum/pacman/zypper/apk/brew) - skipped if `uuidgen` is already # installed # - Writes the alias definition to /etc/profile.d/uuid.sh (system-wide) # - Appends a source line to ~/.bashrc (non-login interactive shells); when # run via `sudo bash`, the invoking user's ~/.bashrc, not root's # # Requirements: sudo, a supported package manager # ============================================================================== set -euo pipefail # ── Configuration ────────────────────────────────────────────────────────────── PROFILE_SCRIPT="/etc/profile.d/uuid.sh" # ── Helpers ──────────────────────────────────────────────────────────────────── err() { echo "[ERROR] $*" >&2; } info() { echo "[INFO] $*"; } # ── Install `uuidgen` via the detected package manager ──────────────────────── install_uuidgen() { if command -v uuidgen &>/dev/null; then info "uuidgen already installed at $(command -v uuidgen) - skipping." return fi if command -v apt-get &>/dev/null; then info "Installing uuid-runtime via apt-get..." sudo apt-get update -qq sudo apt-get install -y uuid-runtime elif command -v dnf &>/dev/null; then info "Installing util-linux via dnf..." sudo dnf install -y util-linux elif command -v yum &>/dev/null; then info "Installing util-linux via yum..." sudo yum install -y util-linux elif command -v pacman &>/dev/null; then info "Installing util-linux via pacman..." sudo pacman -Sy --noconfirm util-linux elif command -v zypper &>/dev/null; then info "Installing util-linux via zypper..." sudo zypper install -y util-linux elif command -v apk &>/dev/null; then info "Installing util-linux via apk..." sudo apk add util-linux elif command -v brew &>/dev/null; then info "Installing util-linux via brew..." brew install util-linux else err "No supported package manager found (apt-get/dnf/yum/pacman/zypper/apk/brew)." err "Install uuidgen manually, then re-run this script." exit 1 fi command -v uuidgen &>/dev/null || { err "uuidgen installation failed."; exit 1; } info "uuidgen installed at $(command -v uuidgen)." } # ── Write the alias definition ───────────────────────────────────────────────── write_alias() { info "Writing alias definition to $PROFILE_SCRIPT..." sudo tee "$PROFILE_SCRIPT" > /dev/null <<'EOF' # uuid alias - managed by uuid-install.sh alias uuid='uuidgen' EOF sudo chmod 644 "$PROFILE_SCRIPT" info "Alias file written: $PROFILE_SCRIPT" # Under `sudo bash`, $HOME is /root, so register in the invoking user's # ~/.bashrc instead, so the alias lands where it'll actually be used. local target_user target_home if [ "$(id -u)" -eq 0 ] && [ -n "${SUDO_USER:-}" ] && [ "$SUDO_USER" != "root" ]; then target_user="$SUDO_USER" target_home=$(getent passwd "$target_user" | cut -d: -f6) else target_user="$(id -un)" target_home="$HOME" fi if [ -z "$target_home" ] || [ ! -d "$target_home" ]; then err "Could not determine home directory for user $target_user." exit 1 fi local bashrc="$target_home/.bashrc" local bashrc_marker="# uuid-install.sh" if grep -q "$bashrc_marker" "$bashrc" 2>/dev/null; then info "$bashrc already sources $PROFILE_SCRIPT." else info "Adding source line to $bashrc for non-login shells..." local created=false [ -e "$bashrc" ] || created=true cat >> "$bashrc" <