000-221 · Question #81
Why does the following ksh code segment fail to run?
The correct answer is C. The 'elseif keyword is not valid within ksh scripts. In the Korn shell (ksh), the correct keyword for else-if branching is 'elif', not 'elseif', so using 'elseif' causes a syntax error that prevents execution.
Question
Why does the following ksh code segment fail to run?
Exhibit
Options
- AAn alphanumeric operator is being used to compare the integer variable.
- BThe 'if statement must be terminated with an 'end1 keyword.
- CThe 'elseif keyword is not valid within ksh scripts.
- DThe segment is missing an 'else' default statement.
How the community answered
(39 responses)- A10% (4)
- B3% (1)
- C79% (31)
- D8% (3)
Why each option
In the Korn shell (ksh), the correct keyword for else-if branching is 'elif', not 'elseif', so using 'elseif' causes a syntax error that prevents execution.
Alphanumeric string operators such as = and != are valid in ksh for string comparisons and do not cause the script to fail when used appropriately.
ksh if statements are terminated with the 'fi' keyword, not 'end1', but the absence of a correct terminator is not the issue causing the described failure.
The Korn shell follows POSIX sh syntax, which defines 'elif' as the only valid else-if keyword within an if-then-fi block. The keyword 'elseif' originates from other languages such as Pascal or PHP and is not recognized by the ksh interpreter, producing a syntax error at parse time. Replacing 'elseif' with 'elif' would resolve the failure.
An 'else' default clause is entirely optional in ksh if statements - omitting it is syntactically valid and does not prevent the script from running.
Concept tested: ksh if-elif-fi conditional syntax in shell scripting
Source: https://www.ibm.com/docs/en/aix/7.3?topic=scripting-using-korn-shell-ksh
Topics
Community Discussion
No community discussion yet for this question.
