تمرين إعادة البناء
File Extension Counter
أعد كتابة سكربت Bash يستخدم associative array لعد امتدادات الملفات.
bash
~9 دقيقة
مبتدئ
أعد بناء الكود
Rebuild
هذا هو الكود. اكتبه بنفسك.
الكود المرجعي
#!/usr/bin/env bash
# Count files by extension.
# Portable to Bash 3.2+ (macOS default) — no associative arrays:
# we emit one extension per line and let `sort | uniq -c` aggregate.
set -euo pipefail
files=(
"main.go"
"README.md"
"lesson.md"
"script.sh"
"archive"
"styles.css"
"notes.md"
)
for file in "${files[@]}"; do
if [[ "$file" == *.* ]]; then
printf '%s\n' "${file##*.}"
else
printf 'no-extension\n'
fi
done \
| sort \
| uniq -c \
| awk '{ printf "%-12s %d\n", $2, $1 }'اكتب هنا