هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
الهدف
Write one real script that uses Part 4: diskreport shows the disk use of directories, warns above a limit, takes options, and cleans up after itself. Check it with ShellCheck and trace it with bash -x.
قبل أن تبدأ
- Make the directory
~/binand check that it is in yourPATH(chapter 24). - Install ShellCheck if you can:
sudo dnf install ShellCheckorsudo apt install shellcheck.
المهام
ضع علامة على المهمة عندما تنتهي منها. متصفحك فقط يتذكر ذلك.
- Make the script
~/bin/diskreportwith a shebang, a comment, and execute permission. - Options:
-l NUMBERsets a warning limit in percent (default 80, also from the environment variableDISK_LIMIT),-o FILEwrites the report to a file,-hprints a usage line. A wrong option prints the usage line to stderr and exits with status 1. - A function
check_limittakes a number and returns 0 if it is below the limit, 1 if not. The limit must be checked to be a whole number. - A function
report_fsreadsdf -Pand prints each filesystem with its use; lines at or above the limit get the wordWARNING. - A function
report_dirsloops over the remaining arguments (directories). For each one that exists, print its size withdu -sh; for others print an error to stderr. Store the sizes in an associative array and print the total number of directories at the end. - Build the report in a temporary file made with
mktemp, remove it with atraponEXIT, and copy it to the-ofile or print it. - Run
shellcheckon the script and fix all warnings. Trace one run withbash -x.
تحقّق من عملك
diskreport -h
diskreport -x; echo "status=$?"
diskreport -l 1 / /etc /nothing
DISK_LIMIT=99 diskreport /tmp
diskreport -o ~/report.txt ~ && cat ~/report.txt
shellcheck ~/bin/diskreport && echo "no warnings"-xprints the usage to stderr andstatus=1.- With
-l 1every filesystem that is 1% or more full showsWARNING;/nothinggives an error message. ~/report.txtcontains the report; no file/tmp/diskreport.*stays behind.
تلميحات
اعرض التلميحات
getopts ":l:o:h", thenshift $((OPTIND - 1)).- Default from the environment:
limit="${DISK_LIMIT:-80}". - Test a whole number:
[[ "$limit" =~ ^[0-9]+$ ]]. df -P | tail -n +2 | while read -r _ _ _ _ pct mnt; do …(_= a column you do not need) and${pct%\%}removes the%.declare -A sizes;sizes["$d"]="$(du -sh "$d" | cut -f1)".tmp="$(mktemp /tmp/diskreport.XXXXXX)"andtrap 'rm -f "$tmp"' EXIT.
الحل
اعرض الحل
- The full script.~/bin/diskreport
#!/bin/bash # diskreport: show filesystem use and directory sizes PROGNAME="${0##*/}" limit="${DISK_LIMIT:-80}" outfile="" usage () { echo "usage: $PROGNAME [-l percent] [-o file] [directory...]" } check_limit () { local value="$1" (( value < limit )) } report_fs () { local pct mnt use echo "== Filesystems (limit ${limit}%) ==" df -P | tail -n +2 | while read -r _ _ _ _ pct mnt; do use="${pct%\%}" if check_limit "$use"; then printf "%-25s %4s%%\n" "$mnt" "$use" else printf "%-25s %4s%% WARNING\n" "$mnt" "$use" fi done } report_dirs () { local d declare -A sizes echo "== Directories ==" for d in "$@"; do if [[ -d "$d" ]]; then sizes["$d"]="$(du -sh "$d" 2> /dev/null | cut -f1)" printf "%-25s %8s\n" "$d" "${sizes[$d]}" else echo "$PROGNAME: not a directory: $d" >&2 fi done echo "Directories checked: ${#sizes[@]}" } while getopts ":l:o:h" opt; do case "$opt" in l) limit="$OPTARG" ;; o) outfile="$OPTARG" ;; h) usage; exit 0 ;; *) usage >&2; exit 1 ;; esac done shift $((OPTIND - 1)) if [[ ! "$limit" =~ ^[0-9]+$ ]]; then echo "$PROGNAME: the limit must be a whole number" >&2 exit 1 fi tmp="$(mktemp /tmp/diskreport.XXXXXX)" || exit 1 trap 'rm -f "$tmp"' EXIT { echo "Disk report for $HOSTNAME - $(date '+%F %R')" report_fs (( $# > 0 )) && report_dirs "$@" } > "$tmp" if [[ -n "$outfile" ]]; then cp "$tmp" "$outfile" else cat "$tmp" fi - Make it executable and test it.
ملاحظاتي
لا توجد ملاحظات بعد.