Unix Questions and Answers – Using test and [ ] to Evaluate Expressions – 2

This set of Unix MCQs focuses on “Using test and [ ] to Evaluate Expressions – 2”.

1. Which one of the following option is used for checking that the string is NULL string?
a) -a
b) -o
c) -z
d) -n
View Answer

Answer: c
Explanation: test can be used to compare strings with another set of operators. -z is used for checking if the string is null. For example,

If [ -z “$fname ] ;
then
echo “NULL string”

2. We can use a test to test various file attributes.
a) True
b) False
View Answer

Answer: a
Explanation: test can also be used to test the various file attributes like its type (ordinary, regular or symbolic link) or its permissions (read, write, execute, etc).
advertisement
advertisement

3. Which option is used for checking if the file exists or not?
a) -e
b) -a
c) -n
d) -f
View Answer

Answer: a
Explanation: test can also be used to test the various file attributes like its type (ordinary, regular or symbolic link). To check whether the file exists or not, we can use the -e option. For example,

If [ -e $1 ]; then
     echo “ file exists”

4. Which of the following option is used for checking if the file is readable or not?
a) -r
b) -f
c) -n
d) -z
View Answer

Answer: a
Explanation: To check if the file exists and is readable we can use -r option. The syntax is -r filename.

5. Which of the following option is used for checking if the file is writable or not?
a) -e
b) -f
c) -n
d) -w
View Answer

Answer: d
Explanation: To check if the file exists and is writable we can use -w option. The syntax is -w filename.
advertisement

6. To check if the file exists and is executable we have to use ___ option with test.
a) -e
b) -f
c) -x
d) -w
View Answer

Answer: c
Explanation: If we want to check whether the file exists and is executable we have to use the -x option. The syntax is -x filename.

7. -d option is used for checking if the file exists and is a directory.
a) True
b) False
View Answer

Answer: a
Explanation: If we want to check whether the file exists and is a directory we have to use the -d option. The syntax is -d filename.
advertisement

8. ____ option is used for checking whether a particular file is older than a specified file.
a) -ef
b) -old
c) -nt
d) -ot
View Answer

Answer: d
Explanation: If we have two file namely file01 and file02 and we want to check if file01 is older than file02, we have to use -ot option with test. The syntax is file01 -ot file02. Similarly, to check whether file01 is newer than file02 we’ve to use -nt option. These commands work in korn and bash shell only.

9. To check if two files are linked to each other, we can use -ef option.
a) True
b) False
View Answer

Answer: a
Explanation: If we have two files namely file01 and file02 and we want to check if file01 is linked with file02, we have to use -ef option with test. The syntax is file01 -ef file02.

Sanfoundry Global Education & Learning Series – Unix.

To practice MCQs on all areas of Unix, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.