nerdexam
CompTIA

XK0-004 · Question #427

An administrator wrote the following script, but it failed: Which of the following is MOST likely the error in the script?

The correct answer is A. Line 1 should read: #!/bin/sh. The shebang line must correctly reference the interpreter. Using #!/bin/bash instead of #!/bin/sh (or vice versa) can cause script failures if the wrong shell is invoked.

Scripting, Containers and Automation

Question

An administrator wrote the following script, but it failed:

Which of the following is MOST likely the error in the script?

Exhibit

XK0-004 question #427 exhibit

Options

  • ALine 1 should read: #!/bin/sh
  • BLine 4 should read: for line in $ { FILES}
  • CLine 6 should read: OWNER-'ls -1 ${LINE} I cut -d " " -f 1'
  • DLine 7 should read: echo '$(line) - S{OWNER)'

How the community answered

(39 responses)
  • A
    74% (29)
  • B
    3% (1)
  • C
    15% (6)
  • D
    8% (3)

Why each option

The shebang line must correctly reference the interpreter. Using #!/bin/bash instead of #!/bin/sh (or vice versa) can cause script failures if the wrong shell is invoked.

ALine 1 should read: #!/bin/shCorrect

The shebang line (#!/bin/bash or #!/bin/sh) determines which shell interpreter executes the script. If the script uses bash-specific syntax but the shebang points to /bin/sh, or is malformed, the interpreter will fail to parse the script correctly. Correcting line 1 to #!/bin/sh ensures the appropriate POSIX shell is used.

BLine 4 should read: for line in $ { FILES}

The for loop variable syntax shown is not a valid correction - standard bash uses $FILES without double braces for variable expansion in loops.

CLine 6 should read: OWNER-'ls -1 ${LINE} I cut -d " " -f 1'

The OWNER assignment using backtick-style command substitution is a style issue, not the root cause of the script failure identified in the question.

DLine 7 should read: echo '$(line) - S{OWNER)'

The echo statement syntax shown mixes quoting styles incorrectly, but this is not the most likely primary cause of the script failing to run at all.

Concept tested: Bash script shebang line interpreter declaration

Source: https://www.gnu.org/software/bash/manual/bash.html

Topics

#bash scripting#shebang line#script syntax#shell errors

Community Discussion

No community discussion yet for this question.

Full XK0-004 Practice