nerdexam
IBM

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.

System Management

Question

Why does the following ksh code segment fail to run?

Exhibit

000-221 question #81 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)
  • A
    10% (4)
  • B
    3% (1)
  • C
    79% (31)
  • D
    8% (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.

AAn alphanumeric operator is being used to compare the integer variable.

Alphanumeric string operators such as = and != are valid in ksh for string comparisons and do not cause the script to fail when used appropriately.

BThe 'if statement must be terminated with an 'end1 keyword.

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.

CThe 'elseif keyword is not valid within ksh scripts.Correct

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.

DThe segment is missing an 'else' default statement.

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

#ksh scripting#shell syntax#elseif#conditional logic

Community Discussion

No community discussion yet for this question.

Full 000-221 Practice