A user is asking the systems administrator for assistance wi - CompTIA Linux + XK0-005

Question

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

Answers
  1. correct
Explanation

Correct Answer: A. if [ -f "$filename" ]; then

The correct syntax to check if a file exists in a shell script is if [ -f "$filename" ]; then. The -f flag tests whether the given path refers to a regular file, and the use of brackets and semicolon (;) is required for proper syntax. This structure ensures the script will conditionally execute a block only if the specified file actually exists and is not a directory.

Why other options are incorrect:

  • B. if [ -d "$filename" ]; then: This checks whether the path is a directory, not a regular file. Since the task is to verify if a file exists, using -d instead of -f makes this condition incorrect for the intended purpose.
  • C. if [ -f "$filename" ] then: This is a syntax error because it omits the required semicolon (;) or newline before then. Without the proper delimiter, the shell will not parse the statement correctly, resulting in a script error.
  • D. if [ -f "$filename" ]; while: This is invalid syntax because while cannot follow an if condition like this. The correct control flow keyword following a conditional check should be then, not while. This would cause the shell script to fail.

Related Courses

Discover a range of courses designed to provide you with the knowledge and skills needed to excel in your chosen field.

a prepsaret exam featured image
CompTIA Prep

220-1201 - CompTIA A+ Exam Core 1

Start Course Prep
a prepsaret exam featured image
CompTIA Prep

220-1202 - CompTIA A+ Exam Core 2

Start Course Prep
a prepsaret exam featured image
CompTIA Prep

CompTIA SecurityX CAS-005

Start Course Prep
a prepsaret exam featured image
CompTIA Prep

CompTIA Cloud Essentials+ CLO-002

Start Course Prep
a prepsaret exam featured image
CompTIA Prep

CompTIA Network+ N10-009

Start Course Prep
a prepsaret exam featured image
CompTIA Prep

CompTIA Security+ SY0-701

Start Course Prep
a prepsaret exam featured image
CompTIA Prep

CompTIA Data + DA0-001

Start Course Prep
a prepsaret exam featured image
CompTIA Prep

CompTIA Pentest+

Start Course Prep
a prepsaret exam featured image
CompTIA Prep

CompTIA CySA+ CSO-003

Start Course Prep
a prepsaret exam featured image
CompTIA Prep

CompTIA DataSys+ DS0-001

Start Course Prep
a prepsaret exam featured image
CompTIA Prep

CompTIA Tech+ FC0-U71

Start Course Prep
a prepsaret exam featured image
CompTIA Prep

CompTIA Cloud+ CV0-004

Start Course Prep
a prepsaret exam featured image
CompTIA Prep

CompTIA Server+ SK0-005

Start Course Prep

Easy way to pass your test within a week with prepsaret

You don’t need one month to study and pass your test.
With Prepsaret, it takes you a few days to grasp all the concepts needed to pass your exams

View Courses Offered