تمرين إعادة البناء
Process Memory Top
أعد كتابة سكربت Bash يعرض أعلى العمليات استهلاكاً للذاكرة في جدول صغير.
bash
~12 دقيقة
متوسط
أعد بناء الكود
Rebuild
هذا هو الكود. اكتبه بنفسك.
الكود المرجعي
#!/usr/bin/env bash
# Top processes by RSS memory.
# `comm` may contain whitespace, which would break a column-3 numeric sort.
# Fix: emit `pid rss comm` (numeric BEFORE name), sort on column 2, then
# reformat for the human-readable table.
set -euo pipefail
limit="${1:-5}"
if [[ ! "$limit" =~ ^[0-9]+$ ]] || ((limit < 1)); then
printf 'Limit must be a positive number.\n' >&2
exit 1
fi
printf '%-8s %-10s %s\n' "PID" "RSS_KB" "COMMAND"
ps -eo pid=,rss=,comm= \
| sort -k2 -nr \
| head -n "$limit" \
| while read -r pid rss command; do
printf '%-8s %-10s %s\n' "$pid" "$rss" "$command"
doneاكتب هنا