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.
Question
An administrator wrote the following script, but it failed:
Which of the following is MOST likely the error in the script?
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)- A74% (29)
- B3% (1)
- C15% (6)
- D8% (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.
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.
The for loop variable syntax shown is not a valid correction - standard bash uses $FILES without double braces for variable expansion in loops.
The OWNER assignment using backtick-style command substitution is a style issue, not the root cause of the script failure identified in the question.
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
Community Discussion
No community discussion yet for this question.
