|
#!/bin/bash |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BLUE='\033[0;34m' |
|
GREEN='\033[0;32m' |
|
RED='\033[0;31m' |
|
NC='\033[0m' |
|
|
|
usage() { |
|
echo "Usage: $(basename $0) [-k size_in_kb] [-c] files..." |
|
echo " -k: Maximum file size in KB (optional)" |
|
echo " -c: Enable color output" |
|
exit 1 |
|
} |
|
|
|
|
|
COLOR=false |
|
MAX_SIZE_KB="" |
|
|
|
|
|
while getopts "k:c" opt; do |
|
case $opt in |
|
k) MAX_SIZE_KB="$OPTARG";; |
|
c) COLOR=true;; |
|
?) usage;; |
|
esac |
|
done |
|
|
|
|
|
shift $((OPTIND-1)) |
|
|
|
|
|
if [ $# -eq 0 ]; then |
|
usage |
|
fi |
|
|
|
|
|
get_file_size() { |
|
if [[ "$OSTYPE" == "darwin"* ]]; then |
|
stat -f %z "$1" |
|
else |
|
stat --format=%s "$1" |
|
fi |
|
} |
|
|
|
|
|
show_header() { |
|
local file="$1" |
|
local size_bytes=$(get_file_size "$file") |
|
local size_kb=$((size_bytes / 1024)) |
|
|
|
if $COLOR; then |
|
echo -e "\n${BLUE}=== File: ${GREEN}$file${BLUE} (${size_kb}KB) ===${NC}" |
|
else |
|
echo -e "\n=== File: $file (${size_kb}KB) ===" |
|
fi |
|
} |
|
|
|
|
|
for file in "$@"; do |
|
if [ ! -f "$file" ]; then |
|
if $COLOR; then |
|
echo -e "${RED}Error: '$file' does not exist or is not a regular file${NC}" >&2 |
|
else |
|
echo "Error: '$file' does not exist or is not a regular file" >&2 |
|
fi |
|
continue |
|
fi |
|
|
|
show_header "$file" |
|
|
|
if [ -n "$MAX_SIZE_KB" ]; then |
|
size_bytes=$(get_file_size "$file") |
|
size_kb=$((size_bytes / 1024)) |
|
|
|
if [ $size_kb -gt $MAX_SIZE_KB ]; then |
|
if $COLOR; then |
|
echo -e "${RED}File size ($size_kb KB) exceeds limit ($MAX_SIZE_KB KB). Showing first $MAX_SIZE_KB KB:${NC}" |
|
else |
|
echo "File size ($size_kb KB) exceeds limit ($MAX_SIZE_KB KB). Showing first $MAX_SIZE_KB KB:" |
|
fi |
|
head -c $((MAX_SIZE_KB * 1024)) "$file" |
|
echo -e "\n[Truncated...]" |
|
else |
|
cat "$file" |
|
fi |
|
else |
|
cat "$file" |
|
fi |
|
done |
|
|