Courseiva
Create simple shell scripts →mediumMultiple Choice

EX200 Create simple shell scripts Practice Question

An administrator writes a script to check disk usage and send an alert if usage exceeds 80%. The script uses 'df -h /' and parses the output. To maintain portability and avoid common pitfalls, which approach is recommended?

⚠ Common exam trap

The trap on the RHCSA exam is that candidates assume `-h` is always better for readability, but the exam tests understanding that human-readable output is unreliable for scripting due to inconsistent column formatting across different Unix/Linux distributions.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

✓

Use 'df / | awk 'NR==2 {print $5}' | tr -d '%'

It uses `df /` (without `-h`) to produce a stable, machine-parseable output where the fifth field (`$5`) is always the percentage used, and `awk` reliably extracts it. The `tr -d '%'` removes the percent sign for numeric comparison. This approach avoids the portability issues of parsing human-readable output from `df -h`, which can vary in column spacing and ordering across different Unix/Linux systems.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✗

    Use 'df -h / | tail -1 | sed 's/.* //' | tr -d '%'

    Why it's wrong here

    Fragile because it parses the human-readable output from 'df -h' with sed. The formatting of 'df -h' can vary across systems; for example, the percentage field may not be the last field, or there may be multiple spaces, making 's/.* //' unreliable. Additionally, using 'tail -1' assumes the output has only two lines, which may not hold if there are multiple mount points in /? This approach is not portable.

  • ✗

    Use 'df -h / | tail -1 | cut -d' ' -f5'

    Why it's wrong here

    This option also uses `-h` and attempts to use `cut` with space delimiter. However, `df -h` output may have multiple spaces or different column ordering, so `cut -d' ' -f5` may not always extract the correct field. This is unreliable for portability.

  • ✓

    Use 'df / | awk 'NR==2 {print $5}' | tr -d '%'

    Why this is correct

    This option uses `df /` without `-h`, which produces a stable, machine-parseable output. The `awk` command extracts the fifth field (the percentage used) and `tr` removes the percent sign. This is portable across different Unix/Linux systems.

  • ✗

    Use 'df -h / | grep -oP '\d+%'

    Why it's wrong here

    This option uses `df -h` and `grep` with a Perl regex to extract digits followed by '%'. While it may work on some systems, the human-readable output can vary, and the regex might not match all cases (e.g., if the percentage is like '100%' or has a leading space). It is less reliable than the approach in C.

About these practice questions

One of 427 original EX200 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This EX200 practice question is part of Courseiva's free Red Hat certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the EX200 exam.