Home » LPI » 117-201 » How would you correctly test the exit status of the previous command in BASH?
You are creating a script with demands that the previous command execute correctly. How would you correctly test the exit status of the previous command in BASH?
A. if [ “$#” -eq “0” ]; then…
B. if [ “$?” -eq “0” ]; then…
C. if [ ‘$#’ == 0 ]; then…
D. if [ ‘$?’ == ‘0’]; then…
E. if [ $@ -eq 0 ]; then…
Correct Answer: B
Explanation/Reference:
Explanation: The variable "$?" checks the exit status of the last command run. The -eq “0” statement is used to check whether a condition is true. The statement if [ “$?” -eq “0” ]; then… will check that the last command executed correctly and run the next part of the script.
Reference: http://www.bolthole.com/solaris/ksh-basics.html
Incorrect Answers
A:The variable is “$?” not “$#”.
C:The variable is “$?” not “$#”.
D:The variable is “$?” not ‘$?’ (double quotes, not single quotes).
E:The variable is “$?” not $@.
Download Printable PDF. VALID exam to help you PASS.
|
|