Friday, July 25, 2014

I/O Redirection in Linux/Unix

I/O Redirection

  • Redirection refers to changing the shell's normal method of handling standard output (stdout), standard input (stdin) and standard error (stderr) for processes. By default, all of these are from/to your screen.
  • All commands expect 3 files to be opened for them when they are invoked:
    STDIN - Standard input
    STDOUT - standard output
    STDERR - Standard error
  • The following symbols are used on the shell command line to redirect a process's stdin, stdout and/or stderr to another location, such as a file or device.
         >              - redirect stdout to output(overwrite)
         >>             - append stdout to output
         <              - redirect stdin 
         2&>            - redirect stderr (sh,ksh,bash) 
         >&             - redirect stdout and stderr (csh,tcsh) 
    

I/O Redirection Examples

Examples:
     

     mail tony < memo           - uses the file memo as input 

                                  to the mail program

     ls -l > my.directory       - redirects output of ls -l 

                                  command to a file called 

                                  my.directory.  If the file

                                  already exists, it is 

                                  overwritten

     cat Mail/jsmith >> Oldmail - appends the contents of 

                                  Mail/jsmith to the file 

                                  Oldmail (does not overwrite)

     myprog >& output           - redirects stdout and stderr 

                                  from myprog's execution to

                                  a file called output

                                  (csh,tcsh)

     (myprog > out) >& err      - redirects stdout from myprog's

                                  execution to a file called out

                                  and stderr to the file err

                                  (csh,tcsh)

     myprog 2> runtime.errors   - redirects stderr from

                                  myprog's execution to a

                                  file called runtime.errors

                                  (sh,ksh,bash)

     myprog > output 2>&        - redirects stderr and stdout

                                  from myprog's execution 

                                  to a file called output

                                  (sh,ksh,bash)

     myprog > out 2> err        - redirects stdout from myprog's

                                  execution to a file called out

                                  and stderr to the file err

                                  (sh,ksh,bash)

0 comments:

Post a Comment