تمرين إعادة البناء
Line Word Counter
أعد كتابة سكربت Bash يحسب عدد الأسطر والكلمات والحروف في نص ثابت.
bash
~8 دقيقة
مبتدئ
أعد بناء الكود
Rebuild
هذا هو الكود. اكتبه بنفسك.
الكود المرجعي
#!/usr/bin/env bash
set -euo pipefail
# <<'TEXT' is a heredoc — content is treated literally (no variable expansion).
# Use <<-TEXT (with a dash) to allow leading tabs to be stripped, which lets
# you indent the heredoc body to match the surrounding code:
# text=$(cat <<-'TEXT'
# Bash rewards small clear steps.
# TEXT
# )
# Note: <<- strips leading TABS only, not spaces.
text=$(cat <<'TEXT'
Bash rewards small clear steps.
Quote variables.
Check inputs before work.
TEXT
)
line_count=$(printf '%s\n' "$text" | wc -l | tr -d ' ')
word_count=$(printf '%s\n' "$text" | wc -w | tr -d ' ')
character_count=${#text}
printf 'Lines: %s\n' "$line_count"
printf 'Words: %s\n' "$word_count"
printf 'Characters: %s\n' "$character_count"اكتب هنا