Files
lab1_git/wcl
2025-10-11 12:18:11 +03:00

49 lines
1.2 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
count_file_stats() {
local file="$1"
# файл?
if [ ! -e "$file" ]; then
echo "Ошибка: файл '$file' не существует" >&2
return 1
fi
# обычн?
if [ ! -f "$file" ]; then
echo "Ошибка: '$file' не является обычным файлом" >&2
return 1
fi
# чтен?
if [ ! -r "$file" ]; then
echo "Ошибка: нет прав на чтение файла '$file'" >&2
return 1
fi
# стр, сл, байт
local lines=$(wc -l < "$file")
local words=$(wc -w < "$file")
local bytes=$(wc -c < "$file")
# рез
echo -e "${lines}\t${words}\t${bytes}\t${file}"
}
# main
main() {
# арг?
if [ $# -eq 0 ]; then
echo "Использование: $0 файл1 [файл2 ...]" >&2
echo "Пример: $0 ./test/wiki_ci ./test/nullfile" >&2
exit 1
fi
# oбработка каждого файла
for file in "$@"; do
count_file_stats "$file"
done
}
# 3апуск основной функции
main "$@"