#!/usr/bin/env bash set -euo pipefail usage() { echo "Использование: $0 --input <файл> [--lang ru] [--min-count 1] [--ignore-case] [--output файл]" } input="" lang="ru" min_count=1 ignore_case=0 output="" while [[ $# -gt 0 ]]; do case "$1" in --input) input="${2:-}"; shift 2 ;; --lang) lang="${2:-}"; shift 2 ;; --min-count) min_count="${2:-}"; shift 2 ;; --ignore-case) ignore_case=1; shift ;; --output) output="${2:-}"; shift 2 ;; -h|--help) usage; exit 0 ;; *) echo "Неизвестный аргумент: $1" >&2; usage >&2; exit 2 ;; esac done if [[ -z "$input" || ! -f "$input" ]]; then echo "Укажите существующий входной файл через --input" >&2 exit 2 fi if ! [[ "$min_count" =~ ^[0-9]+$ ]] || [[ "$min_count" -lt 1 ]]; then echo "--min-count должно быть положительным числом" >&2 exit 2 fi if [[ -n "$output" && "$output" == "$input" ]]; then echo "Выходной файл совпадает с входным" >&2 exit 2 fi if ! command -v aspell >/dev/null 2>&1; then echo "Не найден aspell. Установите его и словарь языка $lang." >&2 exit 2 fi tmp_dir="$(mktemp -d)" trap 'rm -rf -- "$tmp_dir"' EXIT if [[ "$ignore_case" -eq 1 ]]; then tr '[:upper:]' '[:lower:]' < "$input" > "$tmp_dir/source.txt" else cp -- "$input" "$tmp_dir/source.txt" fi if ! aspell --lang="$lang" list < "$tmp_dir/source.txt" > "$tmp_dir/unknown.txt"; then echo "aspell не смог загрузить язык $lang. Проверьте словарь." >&2 exit 2 fi sed '/^[[:space:]]*$/d' "$tmp_dir/unknown.txt" | sort | uniq -c | awk -v min="$min_count" '$1 >= min { print }' | sort -rn > "$tmp_dir/report.txt" if [[ ! -s "$tmp_dir/report.txt" ]]; then echo "Незнакомых слов не найдено" exit 0 fi if [[ -n "$output" ]]; then cp -- "$tmp_dir/report.txt" "$output" echo "Результат: $output" else cat "$tmp_dir/report.txt" fi echo "Незнакомых слов: $(wc -l < "$tmp_dir/report.txt" | tr -d ' ')" >&2