Unix Questions and Answers – Shell Programming using Various Commands – 1

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?
a) for
b) while
c) until
d) for, while, until
View Answer

Answer: d
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?
a) do
b) done
c) then
d) do and done
View Answer

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

advertisement
advertisement

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

Answer: a
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.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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

Answer: d
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?
a) for
b) while
c) case
d) for and while
View Answer

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

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

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

advertisement
$ 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
View Answer

Answer: a
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.
a) set
b) shift
c) cut
d) paste
View Answer

Answer: b
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.

$ echo “$@”
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
View Answer

Answer: c
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?
a) –
b) —
c) ??
d) _
View Answer

Answer: b
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.
a) >>
b) <<
c) !!
d) —
View Answer

Answer: b
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.
a) True
b) False
View Answer

Answer: a
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?
a) cut
b) ls
c) paste
d) mailx
View Answer

Answer: d
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.
a) True
b) False
View Answer

Answer: a
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.
a) kill
b) SIGKILL
c) INT
d) trap
View Answer

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

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.