Standard Streams Redirecting Pipes Other

Redirecting standard I/O & Pipes Lecture 4

Wickus Nienaber

Department of Computer Science COP3353 Introduction to Florida State University

May 31, 2011 Standard Streams Redirecting Pipes Other

Expectations of vi and emacs

Now we have covered three editors - pico, vi, and emacs. It is not realistic to expect any one to remember all the pico, emacs or vi commands. You are expected to know the basic commands that allow you to edit a file (including , and ). You are also expected to know the commands covered in the homework assignments and exercises. 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 flexibility in combining programs and UNIX tools

Standard input redirects Can redirect standard input from a file using <, eg: cat < input.txt It reads: the standard input is being redirected for the program from the file file.txt Instead of reading from the keyboard (normal standard input), it used the file instead and reads the characters in the file 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 file using > cat file.txt > output.txt cal > todaycal Instead of writing the output to the screen (displaying it), the output is written to the file output.txt and todaycal as if those files 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 the same time. Standard Streams Redirecting Pipes Other

Usage of Input/Output

It provides a great deal of flexibility of using commands and utilities in UNIX. We can use ““ to count lines, words, and bytes in a file using input redirection (a different way). When you submit a homework/exercise, you have used input redirection: mail -s ”COP3353 Homework #1 - $USER” nienaber $USER < homework1-ans.txt For example, we can save the output from one program in a file and then further process it by another program Standard Streams Redirecting Pipes Other

Potential Exam Questions

True/false The following command is the same as “cp file1 file2“ assuming that file1 is a UNIX text file [T/F] cat < file1 > file2 When the output from a program is redirected to file1.txt, file1.txt will be created if it does not exist [T/F] In UNIX, only the input or the output of a program can be redirected, but NOT both at the same time [T/F] Standard Streams Redirecting Pipes Other

Potential Exam Questions

True/false The following command is the same as “cp file1 file2“ assuming that file1 is a UNIX text file [T] cat < file1 > file2 When the output from a program is redirected to file1.txt, file1.txt will be created if it does not exist [T] In UNIX, only the input or the output of a program can be redirected, but NOT both at the same time [F] Standard Streams Redirecting Pipes Other

>> and << redirects

The >> operator appends to a file rather than redirecting the output to a file cat textinfo > assign4 prog1.exe >> assign4 prog2.exe >> assign4 cat endfile >> assign4 The << operator reads input until the specified 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. | -r ls -l | cut - 38-80 cat file.txt | Pipes are more efficient as compared to using intermediate files Standard Streams Redirecting Pipes Other

Advance Pipes

du -sc * | sort -n |

the du command is for disk usage (default is in blocks of 512 bytes). The s and c flags are to summarize and give a grand total respectively The sort -n command will sort by numeric value and tail commands print out a few lines at the head or tail of the file 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 file inFile.txt. The command cat has its standard output piped into the standard input of command sort. The command sort has its standard output redirected to file 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 executed can be listed on one line separated 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 scripts Standard Streams Redirecting Pipes Other

Grouping Commands

When you have multiple commands on a simple line, you can also group some of them using (). Then you can redirect the input/output from one group at the same time. For example: (date;who) > logged-in Standard Streams Redirecting Pipes Other

Send output to Multiple places

This will not be on the exams. In some situations, you may want to send the output to the screen and at the same time save it to a file. This can be done using ”“. It reads from the standard input and writes to standard output files. To see logged-in users and also save it to a file active-users, we can do: (who; date) | tee active-users