This set of Unix Multiple Choice Questions & Answers (MCQs) focuses on “Shell Programming using Various Commands – 1”.
1. Which of the following commands let us perform a set of instructions repeatedly?
d) for, while, until
Explanation: 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?
d) do and done
Explanation: 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.
Explanation: 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?
Explanation: 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?
d) for and while
Explanation: 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?
Explanation: 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?
Explanation: 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.
Explanation: 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.
9. Which one of the following is an internal command?
Explanation: 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?
Explanation: 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.
Explanation: 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.
Explanation: 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?
Explanation: 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.
Explanation: 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 <<END //empone.sh is a script >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.
Explanation: 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.
Sanfoundry Global Education & Learning Series – Unix.
To practice all areas of Unix, here is complete set of 1000+ Multiple Choice Questions and Answers.