Thursday, August 23, 2018

File Descriptors and Redirection

File Descriptors:
All I/O, including files, pipes, and sockets, are handled by the kernel via a mechanism called the file descriptor. A file descriptor is a small unsigned integer,an index into a file-descriptor table maintained by the kernel and used by the kernel to reference open files and I/O streams. Each process inherits its own file-descriptor table from its parent. The first three file descriptors are 0, 1, and 2. File descriptor 0 is standard input (stdin), 1 is standard output (stdout), and 2 is standard error (stderr).When you open a file, the next available descriptor is 3, and it will be assigned to the new file.

Redirection:
When a file descriptor is assigned to something other than a terminal, it is called I/O redirection. The shell performs redirection of output to a file by closing the standard output file descriptor 1 (the terminal) and then assigning that descriptor to the file. When redirecting standard input, the shell closes file descriptor 0 (the terminal) and assigns that descriptor to a file. The Bash shells handle errors by assigning a file to the file descriptor 2.

wc < sample.txt  : will take content from the sample.text file.
echo "Hello world" > log.txt : This command will redirect output to be saved in the log.txt file
$ echo "Welcome to Shell Scripting" >> log.txt
This command will append the Hello World text in the log.txt file.

tr '[A-Z]' '[a-z]' < sample.txt
The preceding tr command will read text from the sample.txt file. The tr command will convert all uppercase letters to lower case letters and will print converted text on screen

gcc hello.c 2> error_file
The gcc is a C language compiler program. If an error is encountered during compilation, then it will be redirected to error_file. The > character is used for a success result and 2> is used for error results redirection. We can use error_file for debugging purposes:

$ find . –name "*.sh" > success_file 2> /dev/null
In the preceding example, we are redirecting output or success results to success_file and errors to /dev/null. /dev/null is used to destroy the data, which we do not want to be shown on screen.
$ find . –name "*.sh" &> log.txt
The preceding command will redirect both output and error to log.txt.
$ find . –name "*.sh" > log.tx 2>&1
The preceding command will redirect result to log.txt and send errors to where the output is going, such as log.txt.
$ echo "File needs an argument" 1>&2
The preceding command will send a standard output to the standard error. This will merge the output with the standard error.



The summary of all I/O redirection commands will be as follows:
< sample.txt
The command will take input from sample.txt
> sample.txt
The success result will be stored in sample.txt
>> sample.txt
The successive outputs will be appended to sample.txt
2> sample.txt
The error results will be stored in sample.txt
2>> sample.txt
The successive error output will be appended to sample.txt
&> sample.txt
This will store success and errors, such as in sample.txt
>& sample.txt
This will store success and errors, such as in sample.txt (same as above)
2>&1
This will redirect an error to where output is going
1>&2
This will redirects output to where error is going
>|
This overrides no clobber when redirecting the output
<> filename
This uses the file as both standard input and output if a device file (from /dev)
cat xyz > success_file 2> error_file
This stores success and failure in different files

The following is the summary of various metacharacters: 

 Char
Meaning
Example
Possible output
*
Match with zero or multiple number of any character
$ ls –l *.c file*
Sample.c, hello.c, file1, file_2, filebc
?
Match any single character
$ ls –l file?
filea, fileb, file1
[..]
Match with any single character with in the bracket
$ ls –l file[abc]
filea, fileb,filec
;
Command separator
$cat filea; date
Displays the content of filea and displays the current date and time
|
Pipe two commands
$ cat filea | wc -l
Prints the number of lines of filea
()
Group commands, used when the output of the command group has to be redirected
$ (echo "***x.c***";cat x.c)>out
Redirects the content of x.c with a heading ***x.c*** to the file out

No comments:

Post a Comment