250+ TOP MCQs on Shell Programming using Various Commands

Unix Multiple Choice Questions on “Shell Programming using Various Commands”.

1. Which of the following commands let us perform a set of instructions repeatedly?
a) for
b) while
c) until
d) for, while, until

Answer: d
Clarification: For repeatedly performing a set of instructions, we have to use loops. Shell features three types of loops —while, for and until. All of them repeat the instruction set enclosed by certain keywords.

2. Which of the following keywords are used in while loop?
a) do
b) done
c) then
d) do and done

Answer: d
Clarification: while loop repeatedly performs a set of instructions until the control command returns a true exit status. The general syntax for while loop is:

while  condition is true
do
   commands
done

3. until loop operates with a reverse logic as used in while loop.
a) True
b) False

Answer: a
Clarification: Shell also offers an until statement which operates with a reverse logic used in while. With until the loop body is executed as long as the condition remains false.

4. Which one of the following is used for looping with a list?
a) while
b) until
c) case
d) for

Answer: d
Clarification: The shell’s for loop differs in structure as used in C. for loop doesn’t test a condition but it uses a list instead. The syntax for using for loop is:

for variable in list
do
    commands
done

5. Which of the following loop statements uses do and done keyword?
a) for
b) while
c) case
d) for and while

Answer: d
Clarification: Like while loop, for also uses the keywords do and done, but the additional parameters used in for are variables and list.

6. Which command is used for changing filename extensions?
a) chown
b) rename
c) basename
d) rm

Answer: c
Clarification: basename command is used for changing the extensions of a group of filenames. It extracts the “base” filename from an absolute filename. For example,

$ basename  file01.txt  txt
file01            // txt stripped off

7. Which command is used by the shell for manipulating positional parameters?
a) set
b) cut
c) case
d) paste

Answer: a
Clarification: set statement is an internal command which assigns its arguments to positional parameters $1, $2 and so on. For example, the following command will assign the value 1345 to $1 and 5678 to $2,

$ set 1345  5678
$ _
$ echo “$1 is $1, $2 is $2”
$1 is 1345, $2 is 5678

8. ____ statement is used for shifting arguments left.
a) set
b) shift
c) cut
d) paste

Answer: b
Clarification: shift statement transfers the content of a positional parameter to its immediate lower numbered one. This process continues as many times as shift is invoked. For example, when called once, $2 becomes $1 and $3 becomes $2 and so on.

$ echo “[email protected]”
Wed Jan 8 09:48:44 IST 2017
$ echo $1 $2 $3
Wed Jan 8
$ shift
$ echo $1 $2 $3
Jan 8 09:48:44        // parameters shifted

9. Which one of the following is an internal command?
a) cut
b) expr
c) set
d) Is

Answer: c
Clarification: set statement is an internal command which assigns its arguments to positional parameters $1, $2 and so on. While cut, Is and expr are external commands.

10. Which symbol is used with the set command for command substitution?
a) –
b) —
c) ??
d) _

Answer: b
Clarification: set statement can also be used for command substitution. There can be a problem especially when the output of the command begins with a-.It may happen that set interprets it as an option. To avoid this condition, we have to use — (double hyphen) immediately after set. For example,

$ set -- `ls -lfile01`        //first - now taken care of

11. The ____ allows us to read data from the same file containing the script.
a) >>
b) <<
c) !!
d) —

Answer: b
Clarification: It may happen that the data our program wants to read is fixed and limited. The shell uses << symbol to read data from the same file containing the script. This is referred to as a here document, signifying that the data is here rather than in a separate file.

12. Any command using standard input can take the input from here document.
a) True
b) False

Answer: a
Clarification: The shell uses >> symbol to read data from the same file containing the script. This is referred to as a here document. It allows any command (which uses standard input) to take input from it.

13. Which of the following command doesn’t accept a filename as an argument?
a) cut
b) ls
c) paste
d) mailx

Answer: d
Clarification: The mailx command doesn’t accept any filename as its argument. So if we want to give input to the mailx command, we can use the here document.

14. We can use the here document with interactive programs also.
a) True
b) False

Answer: a
Clarification: Many commands require input from the user. So we can use the here document with interactive programs also. In this manner, we can instruct any script to take the input non-interactively by supplying the input using the here document. For example,

$ empone.sh <manager
>emp.lst            
> END

Hence by using the above commands, we can search for the pattern ‘manager’ in emp.lst .

15. ____ command is the appropriate way to interrupt a program.
a) kill
b) SIGKILL
c) INT
d) trap

Answer: d
Clarification: Any shell script is terminated when the interrupt key is pressed. But this is not the appropriate way of doing so. For interrupting any program we should use the trap command.

Leave a Reply

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