Understanding UNIX I/O Redirection and Pipes

lecture 4 n.w
1 / 7
Embed
Share

Learn about redirecting standard I/O, using pipes, and combining commands in UNIX. Explore how to redirect input/output to/from files, append to files, use pipes to pass output between commands, and execute multiple commands efficiently. Enhance your UNIX skills with these powerful techniques.

  • UNIX
  • I/O redirection
  • Pipes
  • Command line
  • Shell scripting

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Lecture 4 Redirecting standard I/O & Pipes COP 3353 Introduction to UNIX 1

  2. 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) 2

  3. Redirecting standard I/O Standard input and output can be redirected providing a great deal of flexibility in combining programs and unix tools Can redirect standard input from a file using < a.out < input12 any use of stdin will instead use input12 in this example Can redirect standard output to a file using > testprog1 > testout1 cal > todaycal a.out < input12 > testout the stdout of a.out is directed to file testout1 in this example Can also redirect stderr and / or stdout at the same time 3

  4. Appending to a file The >> operator appends to a file rather than redirecting the output to a file cat textinfo >assign4 prog1.exe >>assign4 prog2.exe >>assign4 cat endinfo >>assign4 4

  5. Pipes Pipes allow the standard output of one program to be used as the standard input of another program The pipe operator | takes the input from the command on the left and feeds it as standard input to the command at the right of the pipe Examples ls | sort -r prog1.exe < input.dat | prog2.exe | prog3.exe >output.dat ls -l | cut -c 38-80 Pipes are more efficient as compared to using intermediate files 5

  6. Another Example du -sc * | sort -n | tail The du command is for disk usage (default is in blocks of 512 bytes). The s and c flags are for summarize 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 file respectively http://learnlinux.tsf.org.za/courses/build/shell- scripting/ch01s04.html 6

  7. Separating commands Multiple instructions on one line separate instructions by ; ls -l; cal; date 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 7

Related


More Related Content