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...
IT & Cybersecurity Package Join Premium for Full Access