Geavanceerd
Zoeken inLabs
Zo zoek je
  • Zoek een taak of een tool (lvm, selinux, @podman); filter op niveau en op boek of cursus.
  • Met een leeg zoekvak toont een filter alleen al de labs — bijvoorbeeld alle moeilijke labs van één cursus.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

Lab 36.1The Linux Command Line, Hoofdstuk 36

Part 4 practice: write a disk report script with options

Niveau
Gemiddeld
60 min
Machines
any Linux system
Status
Nog niet begonnen

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

Doel

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.

Voordat je begint

Taken

  1. Make the script ~/bin/diskreport with a shebang, a comment, and execute permission.
  2. Options: -l NUMBER sets a warning limit in percent (default 80, also from the environment variable DISK_LIMIT), -o FILE writes the report to a file, -h prints a usage line. A wrong option prints the usage line to stderr and exits with status 1.
  3. A function check_limit takes a number and returns 0 if it is below the limit, 1 if not. The limit must be checked to be a whole number.
  4. A function report_fs reads df -P and prints each filesystem with its use; lines at or above the limit get the word WARNING.
  5. A function report_dirs loops over the remaining arguments (directories). For each one that exists, print its size with du -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.
  6. Build the report in a temporary file made with mktemp, remove it with a trap on EXIT, and copy it to the -o file or print it.
  7. Run shellcheck on the script and fix all warnings. Trace one run with bash -x.

Controleer je werk

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"
  • -x prints the usage to stderr and status=1.
  • With -l 1 every filesystem that is 1% or more full shows WARNING; /nothing gives an error message.
  • ~/report.txt contains the report; no file /tmp/diskreport.* stays behind.

Hints

Toon de hints

Oplossing

Toon de oplossing
  1. 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
  2. Make it executable and test it.
    chmod 755 ~/bin/diskreport
    diskreport -l 1 / /etc /nothing
    shellcheck ~/bin/diskreport
    bash -x ~/bin/diskreport /tmp 2>&1 | head -20

Mijn notities

Nog geen notities.

Lessen voor dit lab

Commando's in dit lab