nerdexam
CompTIA

XK0-004 · Question #260

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

The correct answer is A. chmod u+x shortscript.sh. Newly created files in Linux do not include the execute bit by default, and chmod u+x adds execute permission for the file owner, enabling direct execution of the script.

Scripting, Containers and Automation

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

(44 responses)
  • A
    89% (39)
  • B
    2% (1)
  • C
    7% (3)
  • D
    2% (1)

Why each option

Newly created files in Linux do not include the execute bit by default, and chmod u+x adds execute permission for the file owner, enabling direct execution of the script.

Achmod u+x shortscript.shCorrect

When a file is created with default permissions, the execute bit is absent, so invoking ./shortscript.sh fails with a 'Permission denied' error. The command chmod u+x shortscript.sh adds the execute (x) permission for the owning user (u), which is the direct fix that enables Joe to run the script using the ./shortscript.sh syntax.

Bsource ./shortscript.sh

source ./shortscript.sh is an alternative invocation that reads and executes the file in the current shell without requiring execute permission, but it is not a setup command that enables the ./shortscript.sh syntax to succeed - it bypasses rather than fixes the permission problem.

Cchmod 155 ~/shortscript.sh

chmod 155 sets permissions to --x r-x r-x, which denies read permission to the owner, preventing the shell from reading and interpreting the script file when it is executed.

Dchgrp shortscript.sh Joe

chgrp changes the group ownership of the file but does not modify any permission bits, so it would not grant the execute permission needed to run the script.

Concept tested: Adding execute permissions with chmod to run scripts

Source: https://www.gnu.org/software/coreutils/manual/html_node/chmod-invocation.html

Topics

#chmod#execute permissions#shell scripts#file permissions

Community Discussion

No community discussion yet for this question.

Full XK0-004 Practice