1 / 17

UNIX Chapter 12 Redirection and Piping

UNIX Chapter 12 Redirection and Piping. Mr. Mohammad Smirat. Introduction. There are default files where a command reads its input from and sends its output and error messages to. In UNIX these three files are known as standard files for the command.

tal
Download Presentation

UNIX Chapter 12 Redirection and Piping

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. UNIX Chapter 12Redirection and Piping Mr. Mohammad Smirat

  2. Introduction • There are default files where a command reads itsinput from and sends its output and error messages to. • In UNIX these three files are known as standard files for the command. • The input, output, and the error of a command can be redirected to other files by using file redirection facilities in UNIX. • This allows us to connect several commands together to perform a complex task that can not be performed by a single command.

  3. Introduction (cont…) • In UNIX, three files are automatically opened by the kernel for every command to read input, send output and error messages. • These files are known as standard input -stdin-, standard output - stdout- and standard error -stderr-. • These files are associated with the terminal on which the command executes.

  4. Input Redirection • Input redirection is accomplished by using the less-than < symbol for input file. This syntax will detach the key board from the standard input -stdin- of command and attach it to input file. • The syntax is as the following:Command < input file • $cat < samplewhere sample is not passed as a command line argument to the cat file. The sample file is the input file where the cat command read from. • The cat command takes input from the standard input if no file is passed as an argument.

  5. Output redirection • command > output file • $cat > sample • The syntax is used to detach the display screen from the standard output -stdout- of the command and attach output file instead. • The standard input of cat remains attached to the keyboard. • When the command is executed, it creates a files called sample whose contents are whatever you type on the keyboard until you press ^D. • $grep “abc” student > abc_fileThe above command sends all lines in the student file that contains the string “abc” to a file called abc_file.

  6. Combine input and output redirection • command < input_file > output_file • $cat < sample > sample1the above command takes its input from the file sample and sends its output to the file sample1.

  7. I/O redirection with file descriptors • As we know the UNIX kernel associate an integer number with every opened file. (0 with input, 1 with output, and 2 with standard error files). • So the following command $cat 1> sample is equivalent to$cat > sample • File descriptor can be used with only Bourne and Korn shells. • $cat 2> error_log takes input from the keyboard, sends output to the display screen, and sends the error messages to error_log file. • $cat file1 file2 file3 1> outfile 2> errfileThe above command takes input from file1, file2, and file3 and send its output to outfile, any error message will be sent to errfile (such as file does not exist or no permission for a file)

  8. I/O redirection with file descriptors (See table 12.1 page311) • $cat file1 file2 file3 1> outfile 2>&1This command sends output and errors to the same file which it is outfile. The string 2>&1 tells the command shell to make descriptor 2 a duplicate of descriptor 1. • $sort 0< students 1> students.sorted 2> sort.errorSorts students (input) into student.sorted (output) and redirect errors to sort.error (errors). If the sort command fails to start, because the file does not exist, the error messages goes to the display screen, not to the sort.error because the stderr is still attached to the screen. • To fix that problem the syntax of the command is done this way:$ sort 2> sort.error 0< students 1> student.sorted

  9. Redirecting without overwriting file contents (appending) $ls -l 1>> outfile 2>> error.msg $cat memo letter >> outfile 2>> error.msg • The above appends the errors of the cat command to the file error.msg which created early from the ls command.

  10. UNIX pipes • The UNIX system allows stdout of a command to be connected to stdin of another command. You can use the pipe character (|) to do so.$command1 | command2 | command3 |……$who | sort | grep “myid” > myfile • The above command sorts the output of who command and sends the lines containing myid to a file called myfile, it is equivalent to:$who > temp1$sort < temp1 > temp2$grep “myid” temp2 > myfile$rm temp1 temp2

  11. Pipes (cont…) • $sort file1 > file2$uniq file2 AND $sort file1 | uniq • will do the same thing. They both sort file1 and display the unique records. No repetition of records.

  12. Redirection and piping combined • we can not use the redirection operators and pipes alone to redirect stdout of a command to a file and connect it to stdin of another command in a single command. • However, we can use the tee utility as the following:$command1 | tee file1…..fileN | command2 • where standard output of command1 is connected to stdin of tee, and tee send it input to file1 through fileN and as stdin of command2.

  13. Redirection and piping combined • $cat names students | grep “john doe” | tee file1 file2 | wc –l • The above command will extract the lines from names and students files that contains the string “john doe”, pipes these lines to the tee utility, which puts copies of these lines in file1 and file2, and sends them to command wc -l.

  14. Error redirection in the C shell • In C shell input, output, and append operators (<,>,>>) works the same as other shells, but the file descriptors can not be used with these operators. • Error redirection works differently in the C shell with the operator >&. • command >& file Will redirect errors and output of the command to the file file. • ls -l >& error.msg • cat file1 file2 |& grep “myid”The output of cat command or any error is fed as input to the grep command. (the stdout and stderr of the cat command are attached to the stdin of the grep command)

  15. nocolobber variable • The C shell has a special built-in variable (noclobber) that allows you to protect your files from being overwritten with output redirection. • When set it prevents overwriting of existing files with output redirection.% set noclobber%cat file1 > file2 will generate error message if file2 does exist, otherwise will create file2 from file1. • %cat file1 >> file2works fine if file2 exists and noclobber is set, but an error message is generated if file2 does not exist. • You can override the noclobber variable affect by using the operators >!, >>!. • %cat file1 >! file2will work if noclobber is set and file2 does exist, the same true for the following command:%cat file1>>! file2will work if file2 does not exist and noclobber was set.

More Related