bash: File Streams

streams

  • In UNIX, many things take the form of a stream of bytes.
  • Real files are like that, because we can read the as a stream of bytes.
  • Each process see a private list of streams that the process can access
  • There is a number for each stream used:
    0,1,2,3,4….
    These numbers are called file descriptors
  • The first 3 streams are used as the process
    • input (by default terminal, keyboard as the input)
    • output (by default terminal, screen as the output)
    • error (by default terminal, screen as the output)
  • The stream names are also often contracted to stdin, stdout, and stderr.

Redirect Streams

  • We can redirect stdout to a file, using the ‘>’ character.
  • When redirecting using >, the file is created if it doesn’t exist.
  • If it does exist, the file is truncated to zero length before anything is sent to it.
  • You can create an empty file by redirecting an empty string to the file:
    printf "" > FILENAME
    or by simply using this:
    >

Appending

  • The >> operator doesn’t truncate the destination file;
    it appends to it:
1$ printf "Line 1\n" > out1.txt
2$ printf "Line 2\n" >> out1.txt
3$ cat out1.txt 
4Line 1
5Line 2
6$

Advanced Redirection

  • Redirecting standard output does not redirect standard error.
    Error messages will still be displayed on your monitor.
  • To send the error messages to a file the redirection operator is preceded by the FD.
  • Both standard output and standard error can be redirected on the same line.
1$ 
2$ printf '%s\n%v\n' OK? Oops! > outfile 2> errorfile
3$ 
4$ cat outfile 
5OK?
6$ cat errorfile 
7bash: printf: `v': invalid format character
8$
  • You can send errors to the /dev/null special file. Everything sent there is discarded.

Redirectin stream to another stream

  • Instead of sending output to a file, it can be redirected to another I/O stream by using:

&N
where N is the number of the file descriptor.

  • The following command sends both standard output and standard error to outfile:
    (note that the order is important)
1$ 
2$ printf '%s\n%v\n' OK? Oops! > outfile 2>&1 
3$ 
4$ cat outfile 
5OK?
6bash: printf: `v': invalid format character
7$ 
  • Order is important!
    In this example, error goes to where output is at the moment (scrren) before it is sent to a file:
1$ 
2$ printf '%s\n%v\n' OK? Oops!   2>&1   > outfile 
3bash: printf: `v': invalid format character
4$ cat outfile 
5OK?
6$ 
  • bash has also a nonstandard syntax for redirecting both standard output and standard error to the same place: &> FILE
    To append both standard output and standard error to FILE, use this:
    &>> FILE

Reading from a stream

  • A command that reads from standard input can have its input redirected from a file:
 1$ 
 2$ echo line-11111  > myfile
 3$ echo line-22222  >> myfile
 4$ echo line-33333  >> myfile
 5$ 
 6$ cat < myfile 
 7line-11111
 8line-22222
 9line-33333
10$