#!/usr/bin/env bash set -euo pipefail usage() { cat <<'EOF' Использование: updates_check.sh [--security] [--quiet] Показывает доступные обновления пакетов. Ничего не устанавливает. --security ограничивает вывод security-обновлениями, где это поддерживается. Код возврата 1 означает наличие обновлений. EOF } security_only=0 quiet=0 while [[ $# -gt 0 ]]; do case "$1" in --security) security_only=1; shift ;; --quiet|-q) quiet=1; shift ;; -h|--help) usage; exit 0 ;; *) echo "Неизвестный аргумент: $1" >&2; usage >&2; exit 2 ;; esac done manager="" if command -v apt-get >/dev/null 2>&1; then manager="apt" elif command -v dnf >/dev/null 2>&1; then manager="dnf" elif command -v pacman >/dev/null 2>&1; then manager="pacman" else echo "Не найден apt-get, dnf или pacman." >&2 exit 2 fi count=0 security=0 listing="" case "$manager" in apt) output="$(apt-get -s -o Debug::NoLocking=1 upgrade 2>/dev/null || true)" if grep -q '^E:' <<< "$output"; then echo "apt-get сообщил об ошибке:" >&2 grep '^E:' <<< "$output" | head -n 5 >&2 exit 2 fi count="$(grep -c '^Inst ' <<< "$output" || true)" security="$(grep '^Inst ' <<< "$output" | grep -ci 'security' || true)" listing="$(grep '^Inst ' <<< "$output" | awk '{print $2, $3, $4}' | head -n 20 || true)" ;; dnf) set +e output="$(dnf -q check-update 2>/dev/null)" rc=$? set -e if ((rc != 0 && rc != 100)); then echo "dnf check-update завершился с кодом $rc" >&2 exit 2 fi count="$(awk 'NF >= 3 && $1 !~ /^Last/ {c++} END {print c + 0}' <<< "$output")" listing="$(awk 'NF >= 3 && $1 !~ /^Last/ {print $1, $2, $3}' <<< "$output" | head -n 20 || true)" set +e sec_output="$(dnf -q check-update --security 2>/dev/null)" sec_rc=$? set -e if ((sec_rc == 100)); then security="$(awk 'NF >= 3 && $1 !~ /^Last/ {c++} END {print c + 0}' <<< "$sec_output")" fi ;; pacman) if command -v checkupdates >/dev/null 2>&1; then output="$(checkupdates 2>/dev/null || true)" else output="$(pacman -Qu 2>/dev/null || true)" fi count="$(grep -c . <<< "$output" || true)" listing="$(head -n 20 <<< "$output")" security=0 ;; esac if ((quiet == 0)); then echo "Менеджер пакетов: $manager" fi echo "Доступно обновлений: $count" if [[ "$manager" != "pacman" ]]; then echo "Из них security: $security" fi if ((security_only == 1 && security == 0)); then if ((quiet == 0)); then echo "Security-обновлений нет." fi exit 0 fi if ((count == 0)); then if ((quiet == 0)); then echo "Система обновлена." fi exit 0 fi if ((quiet == 0 && security_only == 0)) && [[ -n "$listing" ]]; then echo echo "Первые пакеты:" echo "$listing" fi if ((security_only == 1)); then ((security > 0)) && exit 1 exit 0 fi exit 1