250+ TOP MCQs on Using test and [ ] to Evaluate Expressions

Unix MCQs on “Using test and [ ] to Evaluate Expressions”.

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

Answer: c
Clarification: 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

Answer: a
Clarification: 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).

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

Answer: a
Clarification: 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

Answer: a
Clarification: 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

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

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

Answer: c
Clarification: 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

Answer: a
Clarification: 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.

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

Answer: d
Clarification: 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

Answer: a
Clarification: 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.

Leave a Reply

Your email address will not be published. Required fields are marked *