250+ TOP MCQs on Shell Programming using Various Commands

Unix Technical Questions & Answers on “Shell Programming using Various Commands”.

1. Which of the following option is used with a set for debugging shell scripts?
a) -a
b) -x
c) -d
d) -e

Answer: b
Clarification: As we know that set command is used for assigning values to positional parameters, it also serves as debugging tool. For this purpose, we’ve to use -x option with it.

2. Suppose x =10, then what will be the value of x$x$?
a) undefined
b) erroneous
c) 100
d) x10$

Answer: d
Clarification: Since x contains the value 10, and $ symbol is used with any variable for displaying its value. So $x will display 10. Hence the output will be x10$.

3. Given x=10 then,

The given statement is ____
a) True
b) False

Answer: b
Clarification: x$x$ will be equal to x10$ and $x$x will be equal to 1010. Hence they are not equal.

4. A shell script stopped running when we change its name. Why?
a) location of the file changed
b) we can’t change the name of the script
c) $0 was used in the script
d) many possible reasons

Answer: c
Clarification: A shell script will stop running when we change its name if we’d used the positional parameter $0 in it as $0 contains the name of the script file.

5. Where is the exit status of a command stored?
a) $0
b) $>
c) $1
d) $?

Answer: d
Clarification: The exit status of a command is that particular value which is returned by the command to its parent. This value is stored in $?.

6. Which of the following is false?
a) here document provides standard input to any script non interactively
b) read command is used for making scripts interactive
c) $* stores the number of arguments specified
d) && and || are logical operators

Answer: c
Clarification: The shell uses << symbol to read data from the same file containing the script. This is referred to as a here document. read command allows us to take input from the user to make the script interactive. && and || are logical operators which allow conditional execution. $* stores the complete set of positional parameters as a single string.

7. test statement cannot ______
a) compare two numbers
b) compare two strings
c) compare two files
d) check a file’s attributes

Answer: c
Clarification: test works in three ways:
• compare two numbers
• compare two strings
• check a file’s attributes

8. ____ option is used with a test for checking if the file exists and has the size greater than zero.
a) -f
b) -r
c) -e
d) -s

Answer: d
Clarification: test can also be used for performing various file tests like checking whether the file is a regular file, or is it readable, writable or executable. To check whether the file exists and has the size greater than zero, we have to use -s option with test. For example,

9. Every time shift command is used, the leftmost variable is lost.
a) True
b) False

Answer: a
Clarification: shift statement is used for shifting the positional parameters to its immediate lower ones. Every time we use shift, the leftmost variable gets lost; s it should be saved in a variable before using shift.

Leave a Reply

Your email address will not be published.