This set of Unix Assessment Questions and Answers focuses on “Redirection and Pipes – 2”.
1. The category of commands which uses both standard input and standard output are called ____
a) directory oriented commands
b) standard input commands
d) standard output commands
Explanation: UNIX commands can be grouped into four categories:
Commands like ls, pwd that write to standard output
Commands like lp that read from standard input
Filters – commands which use both standard input and standard output (like bc)
2. The contents of file001 are:
What will be the contents of result.txt?
bc < file001 > result.txt
c) 429467296, 1250
Explanation: We can redirect bc’s standard input to come from file001 and save the output in another file named result.txt. bc obtained the expressions from the redirected standard input, processed them and sent out the results to a redirected output stream. Here bc command is used as a filter.
3. Which of the following files are known as special files in UNIX?
d) /dev/null and /dev/tty
Explanation: There are two special files named /dev/null and /dev/tty which is used in UNIX for special purposes. For example, if we want to check whether the program runs successfully without seeing its output on the screen or may not want to save output in a file either. For this purpose, a special file /dev/null is used. It accepts any stream without growing in size. The second file /dev/tty is the one used for indicating one’s terminal.
4. The size of /dev/null is always zero.
Explanation: UNIX has a special file that simply accepts any stream without growing in size. This file is /dev/null. This file is used for redirecting error messages away from the terminal so that they don’t appear on the screen. This file is pseudo-device because like other device files, it’s not associated with any physical device.
5. Which symbol is used to redirect error messages to /dev/null?
Explanation: As we redirect our output using > symbol to other files in simple commands, this technique can also be used to redirect error messages to /dev/null file. For example,
$ cmp file01 file02 > /dev/null
6. Which file is used for indicating terminals?
c) terminal file
d) device file
Explanation: /dev/tty is another special file which is used for representing one’s terminal. Consider user1 is working on terminal /dev/pts/1 and user 2 is working on /dev/pts/2. Then, both the users can refer to their own terminals with the same filename. For example, if user 1 issues the command
$ who > /dev/tty
Then, the list of users is sent to the terminal he is currently using — dev/pts/1. Similarly, user 2 can also issue the same command to see the output on his terminal, /dev/pts/2.
/dev/tty can be accessed by multiple users without conflict.
7. Which symbol is used to connect different streams?
Explanation: The shell can connect different streams using a special operator called pipe (|) and avoid creation of disk file. We can connect two commands using this operator so that one command can take the output of other as its input. For example,
$ who | wc -l // count number of lines in the output of who command.
8. We can use the > symbol along with pipe in a command.
Explanation: There is no restriction on the number of commands we can use in a pipeline except that we should know the behavioral properties of these commands to place there. For example, consider this command:
$ ls | wc -l > output.txt // number of lines counted by wc command from the output of ls //are stored in a file named output.txt.
9. What is the output of the following command?
$ wc -c *.c
a) total size of .c files
b) size of each .c file
Explanation: We can make the command ignorant of its source using the pipeline. Above command is also an example of the same. It will simply display the total size of those files having a .c extension in the current directory.
10. In a pipeline, the command on the left of | must use standard output and one on the right must use standard input.
Explanation: There is a restriction which is applied while using a pipeline. In a pipeline, the command on the left of | must use standard output (stream) and one on the right must use standard input (stream). Otherwise, an error will be generated or the output will be erroneous.
11. Which command saves one copy of character stream in a file and store the other to standard output?
Explanation: tee command handles a character stream by duplicating its input. It saves one copy in a file and writes the other to standard output. For example,
$ who | tee user.txt
Above command will the save the output of who command in a file named user.txt as well as it will also display the output of who command on the terminal.
12. tee is an internal command.
Explanation: tee is an external command and not a feature of the shell. tee command handles a character stream by duplicating its input. It saves one copy in a file and writes the other to standard output.
13. Which of the following is not true about tee command?
a) it is a feature of the shell
b) it is an external command
c) tee command duplicates its input
d) tee is an internal command
Explanation: tee command is not a feature of the shell. tee command handles a character stream by duplicating its input. It saves one copy in a file and writes the other to standard output. It is an external command.
14. Apart from its use in redirection, /dev/tty can also be used as an argument to some commands.
Explanation: /dev/tty can be used as an argument to some UNIX commands. Because the terminal is also a file we can use the device name /dev/tty as an argument to some commands like tee. For example,
$ who | tee /dev/tty | wc -l // same as who | wc -l
In the above command, tee saved the output of who in /dev/tty, thus effectively showing it on the screen and wc -l counted the number of lines from another stream.
Sanfoundry Global Education & Learning Series – Unix.
To practice all areas of Unix Assessment Questions, here is complete set of 1000+ Multiple Choice Questions and Answers.