nerdexam
CompTIA

XK0-005 · Question #1488

A user is asking the systems administrator for assistance with writing a script to verify whether a file exists. Given the following: Which of the following commands should replace the <CONDITIONAL>…

The correct answer is A. if [ -f "$filename" ]; then. The command if [ -f "$filename" ]; then checks if the variable $filename refers to a regular file that exists. The -f option is used to test for files. If the condition is true, the commands after then are executed. This is the correct way to replace the <CONDITIONAL> string…

Scripting, Containers, and Automation

Question

A user is asking the systems administrator for assistance with writing a script to verify whether a file exists. Given the following: Which of the following commands should replace the <CONDITIONAL> string?

Exhibit

XK0-005 question #1488 exhibit

Options

  • Aif [ -f "$filename" ]; then
  • Bif [ -d "$filename" ]; then
  • Cif [ -f "$filename" ] then
  • Dif [ -f "$filename" ]; while

How the community answered

(43 responses)
  • A
    88% (38)
  • B
    2% (1)
  • C
    7% (3)
  • D
    2% (1)

Explanation

The command if [ -f "$filename" ]; then checks if the variable $filename refers to a regular file that exists. The -f option is used to test for files. If the condition is true, the commands after then are executed. This is the correct way to replace the <CONDITIONAL> string. The other options are incorrect because they either use the wrong option (-d tests for directories), the wrong syntax (missing a semicolon after the condition), or the wrong keyword (while is used for loops, not

Topics

#Shell Scripting#Bash#Conditional Statements#File System Tests

Community Discussion

No community discussion yet for this question.

Full XK0-005 Practice