#!/usr/bin/env bash set -euo pipefail file="${HOME}/.productivity/grocery.txt" usage() { echo "Использование: $0 [--file PATH] add TEXT | list | done N | remove N --yes | clear --yes" } if [[ "${1:-}" == "--file" ]]; then file="${2:-}" shift 2 fi command="${1:-}" shift || true mkdir -p "$(dirname "$file")" case "$command" in add) text="$*" if [[ -z "$text" ]]; then echo "Укажите название покупки" >&2 exit 2 fi printf '[ ] %s\n' "$text" >> "$file" echo "Добавлено: $text" ;; list) if [[ ! -s "$file" ]]; then echo "Список пуст." exit 0 fi awk '{ printf "%2d. %s\n", NR, $0 }' "$file" ;; done|remove) number="${1:-}" if [[ ! "$number" =~ ^[0-9]+$ || "$number" -lt 1 ]]; then echo "Укажите номер строки" >&2 exit 2 fi if [[ ! -f "$file" ]]; then echo "Файл списка не найден: $file" >&2 exit 1 fi total="$(wc -l < "$file" | tr -d ' ')" if (( number > total )); then echo "Номер вне списка" >&2 exit 1 fi if [[ "$command" == "remove" && "${2:-}" != "--yes" ]]; then echo "Удаление меняет список. Добавьте --yes." >&2 exit 1 fi cp "$file" "$file.bak" temp="$(mktemp)" if [[ "$command" == "done" ]]; then awk -v n="$number" 'NR == n { sub(/^\[ \]/, "[x]") } { print }' "$file" > "$temp" else awk -v n="$number" 'NR != n { print }' "$file" > "$temp" fi mv "$temp" "$file" echo "Обновлено: строка $number" ;; clear) if [[ "${1:-}" != "--yes" ]]; then echo "Очистка меняет список. Добавьте --yes." >&2 exit 1 fi if [[ -f "$file" ]]; then cp "$file" "$file.bak" : > "$file" echo "Список очищен, копия: $file.bak" fi ;; *) usage >&2 exit 2 ;; esac