Вы находитесь на странице: 1из 9

Standard Streams

Redirecting

Pipes

Other

Redirecting standard I/O & Pipes Lecture 4


Wickus Nienaber
Department of Computer Science COP3353 Introduction to Unix Florida State University

September 16, 2008

Standard Streams

Redirecting

Pipes

Other

Standard input, output and error

standard input (0:stdin)


The default place where a process reads its input (usually the terminal keyboard)

standard output (1:stdout)


The default place where a process writes its output (typically the terminal display)

standard error (2:stderr)


The default place where a process can send its error messages (typically the terminal display)

Standard Streams

Redirecting

Pipes

Other

Redirecting standard I/O


Redirecting Standard input and output can be redirected providing a great deal of exibility in combining programs and UNIX tools Standard input redirects Can redirect standard input from a le using <, eg: cat < input.txt
It reads: the standard input is being redirected for the program cat from the le file.txt Instead of reading from the keyboard (normal standard input), it used the le instead and reads the characters in the le as if they are being typed from the keyboard.

Standard Streams

Redirecting

Pipes

Other

Redirecting standard I/O cont.


Standard output redirect Can redirect standard output to a le using > cat file.txt > output.txt cal > todaycal
Instead of writing the output to the screen (displaying it), the output is written to the le output.txt and todaycal as if those les are the screen.

Combining stdin and stdout Can use redirects together (order not important)
cat < infile.txt > outfile.txt or cat > outfile.txt < infile.txt

Can also redirect stderr and/or stdout at the same time.

Standard Streams

Redirecting

Pipes

Other

>> and << redirects


The >> operator appends to a le rather than redirecting the output to a le cat textinfo > assign4 prog1.exe >> assign4 prog2.exe >> assign4 cat endfile >> assign4 The << operator reads input until the specied token is reached: [my prompt]$ cat << end ?hello ?world ?end hello world [my prompt]$

Standard Streams

Redirecting

Pipes

Other

Pipes

Pipes allow the standard output of one program to be used as the standard input of another program. The pipe operator, written as |, takes the output from a command on its left and feeds it as standard input to the command on the right of the pipe.
ls | sort -r ls -l | cut -c 38-80 cat file.txt | more

Pipes are more efcient as compared to using intermediate les

Standard Streams

Redirecting

Pipes

Other

Advance Pipes

du -sc * | sort -n | tail the du command is for disk usage (default is in blocks of 512 bytes). The s and c ags are to summerize and give a grand total respectively The sort -n command will sort by numeric value head and tail commands print out a few lines at the head or tail of the le respectively

Standard Streams

Redirecting

Pipes

Other

Pipes and redirects


cat < inFile.txt | sort -r > finalFile.txt Pipes and redirects can be combined together The command cat has its standard input redirected from le inFile.txt. The command cat has its standard output piped into the stadard input of command sort. The command sort has its standard output redirected to le finalFile.txt. Trying to redirect and pipe the same standard input or output will not work: cat inFile.txt | more < anotherFile.txt

Standard Streams

Redirecting

Pipes

Other

Separating commands

Multiple commands to be exectued can be listed on one line seperated by ;


ls -l; cal; date cd directory; ls-l

Suppose you need to continue a command to the next line, use the \ to do so and then continue your command on the next line
cat filename | sort \ | wc Usually seen in shell scripts

Вам также может понравиться