You are writing a portable shell script that must run under /bin/sh on several Linux servers. The script counts the number of available security updates and stores the result in a variable named updates. If the count is not equal to 0, the script should call a notify_updates function. A fragment of the script looks like:
updates=$(dnf check-update --security 2>/dev/null | grep -vc '^$')
if [ _____ ]; then # TODO: insert test expression
notify_updates "$updates"
fi
Which test expression should replace the blank so the condition is satisfied only when updates is numerically not equal to 0, using the single-bracket POSIX test ([ ]) builtin?
When the single-bracket test builtin is invoked, the operator -ne performs a numeric comparison and returns a zero exit status when the two integer operands are not equal. Therefore, the expression "$updates" -ne 0 is true whenever the update count differs from zero, correctly triggering the call to notify_updates.
The != operator is a string comparison in POSIX test, so it performs a lexical, not numeric, check and is not recommended for integer values. -ge 0 would be true even when the count is 0, while -eq 0 is true only when the count is zero-both fail to meet the requirement. Hence only the -ne comparison fulfills the specification.
Ask Bash
Bash is our AI bot, trained to help you pass your exam. AI Generated Content may display inaccurate information, always double-check anything important.
What does the `-ne` operator do in POSIX test?
Open an interactive chat with Bash
Why is `!=` not suitable for numeric comparisons in POSIX test?
Open an interactive chat with Bash
What is the purpose of `2>/dev/null` in the command?
Open an interactive chat with Bash
CompTIA Linux+ XK0-006 (V8)
Automation, Orchestration, and Scripting
Your Score:
Report Issue
Bash, the Crucial Exams Chat Bot
AI Bot
Loading...
Loading...
Loading...
Pass with Confidence.
IT & Cybersecurity Package
You have hit the limits of our free tier, become a Premium Member today for unlimited access.
Military, Healthcare worker, Gov. employee or Teacher? See if you qualify for a Community Discount.
Monthly
$19.99
$19.99/mo
Billed monthly, Cancel any time.
3 Month Pass
$44.99
$14.99/mo
One time purchase of $44.99, Does not auto-renew.
MOST POPULAR
Annual Pass
$119.99
$9.99/mo
One time purchase of $119.99, Does not auto-renew.
BEST DEAL
Lifetime Pass
$189.99
One time purchase, Good for life.
What You Get
All IT & Cybersecurity Package plans include the following perks and exams .