24 jun 2010

Capturing outputs in shells

*NIX systems make easy to capture program output in separate files using pipes and some commands.

  • Writing standard output from the ls program to the file1 file:


    ls > file1
          
  • Writing error output from the ls program to the file1 file:


    ls 2> file1
          
  • Writing both standard and error output from the ls program to the file1 file:


    ls &> file1
          
  • Writing standard output from the ls program to the file1 file and error output from this command to the file2 file:


    ls 1> file1 2> file2 
In UNIX and Linux shells, a pipe is represented by the "|" symbol and connects the standard output of one command to the standard input of another. The UNIX or Linux tee application does what its name suggests if you're familiar with various plumbing connectors—it splits its input into two different streams. One of these remains standard output, while the other is a file whose name you specify on the command line. Therefore, you can use the combination of a pipe and the tee command to combine redirecting the output of a program into a file and see that output.

For example, the following command displays both the output of the ls /etc command and captures the standard output of that command in the ls.out file:

ls /etc | tee ls.out
  

You can also redirect standard error and standard output via a pipe on the command line by using the "|&" characters. For example, the following command both displays the output and any error messages of the ls /etc command and uses the tee command to capture the output and error messages in the ls.out file:
ls /etc |& tee ls.out

You can combine redirection of standard output and standard error via a pipe, the tee command, and your favorite shell's interactive mode to capture all input, output, and error messages to a file using a command such as the following:

bash -i |& tee output_file.txt
  

Source: http://www.ibm.com/developerworks/aix/library/au-screenshots1/index.html?ca=dgr-lnxw13LX-ScrnShotsdth-LX from William von Hagen

No hay comentarios:

Publicar un comentario