#!/usr/bin/env bash set -euo pipefail usage() { echo "Использование: $0 --input <файл> [--lines 1000] [--out папка] [--write] [--force]" } input="" lines=1000 out="split-out" write=0 force=0 while [[ $# -gt 0 ]]; do case "$1" in --input) input="${2:-}"; shift 2 ;; --lines) lines="${2:-}"; shift 2 ;; --out) out="${2:-}"; shift 2 ;; --write) write=1; shift ;; --force) force=1; shift ;; -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 ! [[ "$lines" =~ ^[0-9]+$ ]] || [[ "$lines" -lt 1 ]]; then echo "--lines должно быть положительным числом" >&2 exit 2 fi if ! command -v split >/dev/null 2>&1; then echo "Не найдена утилита split" >&2 exit 2 fi total_lines="$(awk 'END { print NR }' "$input")" parts=$(( (total_lines + lines - 1) / lines )) base="$(basename -- "$input")" base="${base%.*}" prefix="$out/${base}-part-" echo "Строк: $total_lines, частей по $lines: $parts" echo "Префикс файлов: $prefix" if [[ "$write" -eq 0 ]]; then echo "Это предпросмотр. Для записи добавьте --write." exit 0 fi mkdir -p -- "$out" if compgen -G "${prefix}*" >/dev/null; then if [[ "$force" -eq 0 ]]; then echo "Файлы с таким префиксом уже есть. Добавьте --force для перезаписи." >&2 exit 2 fi rm -f -- "${prefix}"* fi split -l "$lines" -a 3 -- "$input" "$prefix" created="$(find "$out" -maxdepth 1 -type f -name "$(basename -- "$prefix")*" | wc -l | tr -d ' ')" echo "Создано файлов: $created"