#!/bin/bash # ============================================================================== # igrep-hgrep-install.sh # # Installs the `igrep` and `hgrep` shell aliases system-wide. # # Usage: # curl -fsSL https://curl-pipe-bash.watzinger.biz/igrep-hgrep-install.sh | sudo bash # # What it does: # - igrep: alias for `grep --color=auto -i` (no extra package required) # - hgrep: alias for `ag --passthru --color-match "1;31" --color-line-number=0 --color-path=0 --color` # - Installs the-silver-searcher (`ag`), which hgrep depends on, via the # detected system package manager (apt/dnf/yum/pacman/zypper/apk/brew) - # skipped if `ag` is already installed # - Writes the alias definitions to /etc/profile.d/igrep-hgrep.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/igrep-hgrep.sh" # ── Helpers ──────────────────────────────────────────────────────────────────── err() { echo "[ERROR] $*" >&2; } info() { echo "[INFO] $*"; } # ── Install `ag` (the-silver-searcher) via the detected package manager ─────── install_ag() { if command -v ag &>/dev/null; then info "ag already installed at $(command -v ag) - skipping." return fi if command -v apt-get &>/dev/null; then info "Installing silversearcher-ag via apt-get..." sudo apt-get update -qq sudo apt-get install -y silversearcher-ag elif command -v dnf &>/dev/null; then info "Installing the_silver_searcher via dnf..." sudo dnf install -y the_silver_searcher elif command -v yum &>/dev/null; then info "Installing the_silver_searcher via yum..." sudo yum install -y the_silver_searcher elif command -v pacman &>/dev/null; then info "Installing the_silver_searcher via pacman..." sudo pacman -Sy --noconfirm the_silver_searcher elif command -v zypper &>/dev/null; then info "Installing the_silver_searcher via zypper..." sudo zypper install -y the_silver_searcher elif command -v apk &>/dev/null; then info "Installing the_silver_searcher via apk..." sudo apk add the_silver_searcher elif command -v brew &>/dev/null; then info "Installing the_silver_searcher via brew..." brew install the_silver_searcher else err "No supported package manager found (apt-get/dnf/yum/pacman/zypper/apk/brew)." err "Install the-silver-searcher (ag) manually, then re-run this script." exit 1 fi command -v ag &>/dev/null || { err "ag installation failed."; exit 1; } info "ag installed at $(command -v ag)." } # ── Write the alias definitions ──────────────────────────────────────────────── write_aliases() { info "Writing alias definitions to $PROFILE_SCRIPT..." sudo tee "$PROFILE_SCRIPT" > /dev/null <<'EOF' # igrep/hgrep aliases - managed by igrep-hgrep-install.sh alias igrep='grep --color=auto -i' alias hgrep='ag --passthru --color-match "1;31" --color-line-number=0 --color-path=0 --color' EOF sudo chmod 644 "$PROFILE_SCRIPT" info "Alias file written: $PROFILE_SCRIPT" # Under `sudo bash`, $HOME is /root - register in the invoking user's # ~/.bashrc instead, so the aliases land where they'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="# igrep-hgrep-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" <