Linux distributions have three basic forms of Input/Output (standard input, stan
ID: 3727973 • Letter: L
Question
Linux distributions have three basic forms of Input/Output (standard input, standard output, and standard error) redirections which will need to understand how to use each one. State the purpose of each of three I/O forms including which file descriptor is used for each one Provide your own redirection command syntax example as well as description what your command syntax will do (optional-challenging) How would command syntax appear if you want redirect the cat command standard ouput and standard error to the same file? This would be useful if you want to view results of the cat command in a file containing both regular output and any error(s) which might appear (ex. syntax error).
Explanation / Answer
In Linux/Unix systems, there are three default files which are also identified by the shell using file descriptor(FD) numbers:
1.stdin or 0 – it’s connected to the keyboard, to read input
2.stdout or 1 – it’s attached to the screen, to send results and
3.stderr or 2 – the programs send status/error messages
0,1,2 are the FD numbers of stdin,stdout,stderr .
I/O redirection allows to alter the input source of a command as well as where its output and error messages are sent to,by using ‘<’ and ‘>’ redirection operators.
1)The '<' symbol is used for input(STDIN) redirection
Example: The mail program in Linux can help send emails from the Terminal.
If we want to attach a File to email we can use the input re-direction operator in the following format.
Mail –s “Subject” to-address < Filename
hari@VirtualBox:~$ mail –s “News Today” abc@ymail.com <NewsFlash
News Today is the subject of E-mail,
abc@ymail.com is E-mail and
NewsFlash is the file to be attached to mail.
2)The '>' symbol is used for output(STDOUT) redirection
We can redirect standard output to files .That is, we store the output of the top command for later inspection:
$ top –bn 5 >top.log
The flags:
-b - enables top to run in batch mode, so that you can redirect its output to a file or another command.
-n - specifies the number of iterations before the command terminates.
We can view the contents of top.log file using cat command
$ cat top.log
To redirect the output of a command, we use the ‘>>’operator.
The output of top command is redirected to the top.log file on the command line
$ top –bn 5 >>top.log
Using the file descriptor number also the output can be redirected.
$ top –bn 5 1>top.log
3)Error Redirection
By default, error stream is displayed on the screen. Error redirection is routing the errors to a file other than the screen.
The solution is to re-direct the error messages to a file.
$ myproram 2>errorfile
The file descriptor for standard error is 2.
Using "2>" we re-direct the error output to a file named "errorfile"
Thus, program output is not cluttered with errors.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.