nerdexam
CompTIA

XK0-005 · Question #10210

Joe, a user, creates a short shell script, shortscript.sh, and saves it in his home directory with default permissions and paths. He then attempts to run the script by typing ./shortscript.sh, but…

The correct answer is A. chmod u+x shortscript.sh. To execute a shell script, it must have execute permissions, which can be granted using the chmod command.

Troubleshooting

Question

Joe, a user, creates a short shell script, shortscript.sh, and saves it in his home directory with default permissions and paths. He then attempts to run the script by typing ./shortscript.sh, but the command fails to execute. Which of the following commands would have allowed the script to run?

Options

  • Achmod u+x shortscript.sh
  • Bsource ./shortscript.sh
  • Cchmod 155 ~/shortscript.sh
  • Dchgrp shortscript.sh Joe

How the community answered

(55 responses)
  • A
    95% (52)
  • B
    2% (1)
  • C
    4% (2)

Why each option

To execute a shell script, it must have execute permissions, which can be granted using the `chmod` command.

Achmod u+x shortscript.shCorrect

When a script is created, by default it typically does not have execute permissions. The `chmod u+x shortscript.sh` command adds execute permission for the user (u) who owns the file, which is necessary for the script to be runnable by that user using `./shortscript.sh`.

Bsource ./shortscript.sh

source ./shortscript.sh would execute the script in the current shell context but does not directly address the missing execute permission causing the failure.

Cchmod 155 ~/shortscript.sh

chmod 155 ~/shortscript.sh would set permissions to `---r-xr-x`, which removes write permission for the owner and grants execute permission only to others, not directly to the owner for execution.

Dchgrp shortscript.sh Joe

chgrp shortscript.sh Joe changes the group ownership of the file to `Joe`, but this does not affect its executability.

Concept tested: Granting execute permissions to shell scripts

Source: https://man7.org/linux/man-pages/man1/chmod.1.html

Topics

#File Permissions#chmod#Shell Scripting#Troubleshooting Scripts

Community Discussion

No community discussion yet for this question.

Full XK0-005 Practice