You are writing a portable Bourne-compatible shell script that first counts running httpd processes:
proc_count=$(pgrep -c httpd)
The script must continue only when no such processes are running. Which test command, placed inside an if statement, will correctly make the then branch execute only when proc_count is exactly 0?
The expression [ "$proc_count" -eq 0 ] invokes the POSIX test (or [) utility and uses the -eq arithmetic operator, whose specification states "n1 -eq n2 - True if the integers n1 and n2 are algebraically equal." This returns exit status 0 only when the variable's value is the integer 0.
[[ "$proc_count" == 0 ]] performs a string comparison, so values like "00" would also match and it is not POSIX-portable. [ $proc_count -ne 0 ] is the logical opposite of what is needed, succeeding when at least one process is running. (( proc_count = 0 )) assigns 0 to the variable; the assignment's result is 0, yielding a false exit status, so the branch would never run.
Therefore, only the first test fulfills the requirement accurately and portably.
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 is the difference between `[ "$proc_count" -eq 0 ]` and `[[ "$proc_count" == 0 ]]`?
Open an interactive chat with Bash
What does the `-ne` operator in `[ $proc_count -ne 0 ]` do?
Open an interactive chat with Bash
Why is `(( proc_count = 0 ))` incorrect in this scenario?
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