CTEC 1430 Enterprise Computing I - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

CTEC 1430 Enterprise Computing I

Description:

Appending. Special case of redirection STDOUT. Use ' ' instead of ' ... file and then directing output to it, output appended to end of the output file: ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 14
Provided by: rob1177
Category:

less

Transcript and Presenter's Notes

Title: CTEC 1430 Enterprise Computing I


1
CTEC 1430 Enterprise Computing I
  • 4. UNIX I/O Redirection

2
I/O Redirection Filters
  • Recall
  • each UNIX command is written as a general purpose
    program
  • Inputs come from STDIN outputs go to STDOUT

3
File Descriptors
  • When a UNIX command is invoked, shell opens 3 I/O
    files
  • Unless you specify differently, the default files
    will be opened
  • These default files can easily be changed when
    the command is invoked

4
Redirecting Output
  • E.g. redirect output of ls to go into a disk file
    instead of going to the screen
  • ls gt MYFILE
  • To see the contents of MYFILE
  • cat MYFILE
  • .profile
  • adir
  • bdir
  • report
  • MYFILE

5
Redirecting Output (2)
  • When ls output is redirected to MYFILE, shell
    opens the following file descriptors

6
Redirecting Input
  • Not as common as output redirection
  • Most commands take input from files, rather than
    STDIN
  • If command isnt provided a file, input will come
    from the keyboard until a Ctrl-D is read.
  • Example of a command using STDIN as the input
    file
  • cat
  • Some text is typed here.
  • This text will be buffered and sent to STDOUT
    when a Ctrl-D is read
  • D
  • Some text is typed here.
  • Or you could put the above text into MYFILE and
    direct cat to use MYFILE as STDIN
  • cat lt MYFILE

7
Redirecting Diagnostic Output
  • E.g. send error messages from program to
    ERRORFILE, rather than screen
  • cc thisfile.c 2gtERRORFILE
  • For STDERR you must precede the gt by the file
    descriptor number 2
  • The file descriptor for STDOUT is number 1, and
    is optional for redirecting
  • Assumed if not provided

8
I/O Redirection
  • The following commands are equivalent
  • cat 0ltMYFILE
  • cat ltMYFILE
  • cat lt MYFILE
  • catltMYFILE
  • The following commands are equivalent
  • ls 1gtMYFILE
  • ls gtMYFILE

9
Appending
  • Special case of redirection STDOUT
  • Use gtgt instead of gt
  • Instead of wiping out output file and then
    directing output to it, output appended to end of
    the output file
  • cat MYFILE
  • THIS TEXT EXISTS.
  • pwd gtgt MYFILE
  • cat MYFILE
  • THIS TEXT EXISTS.
  • /usr/user1

10
Pipes
  • Pipes take output of one command as input to
    another command
  • Example sort MYFILE, and send it to the lpr
    program to be spooled to the printer
  • cat MYFILE sort lpr

11
I/O Redirection vs. Pipes
12
Filters
  • Most UNIX commands are filters
  • Filters take input from STDIN and put their
    output to STDOUT
  • Filter commands can occur between two 's in a
    pipeline, e.g. print

13
Filters (2)
  • tee
  • program which sends its inputs to both STDOUT and
    a file, e.g. try
  • sort file.txt tee sorted.txt
  • tee can also be used as a filter
Write a Comment
User Comments (0)
About PowerShow.com