Miscellaneous
  Home arrow Miscellaneous arrow Page 2 - Data Streams and the UNIX Shell
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MISCELLANEOUS

Data Streams and the UNIX Shell
By: Gabor Bernat
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2009-05-13

    Table of Contents:
  • Data Streams and the UNIX Shell
  • Pipes
  • Redirection and the Here Document
  • Saving Streams into Variables

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Data Streams and the UNIX Shell - Pipes


    (Page 2 of 4 )

    Pipes were introduced in the early years of the 1970s by Doug McIlroy. These are streams made by the operating system itself. With pipes, you can redirect the output of one command to the input of another with a simple syntax on the command line. The symbol used for a pipe is |.

    For example, below we will sort the first ten lines of a file. For this, we need to give to the sort command only the first ten lines. We will use the head command for this task. We will direct the output of the head command to the input of the sort command with the pipe:

    head -10 | sort

    The operating system will assure us of the synchronization of the data stream between the two functions. You can use the pipe as many times as you wish on a command line. For instance, you can read in the whole file, sort that and print out the last two lines in a single command line.

    Sometimes you may want to save/print to a file the content of a partial result inside a sequence of pipes. For this, you can use the tee function. Its use is simple. After the name of the function, add the name of the file where you want to save the stream. The command will print first to the file and then to the output. For example, let us read, sort, save and print the first item of a file:

    cat alfa.txt | sort | tee sorted_alfa.txt | head -1

    Sometimes we just want to dump a data stream. If this is the case, we can use the special devices instead of files. A device like this is /dev/null. This is a very greedy device. It will eat up all the character sequences you can throw at it. This is the place where you should direct any data stream that you do not want to use.

    A stream like this can be the errors that show up in a command line. The cat command will list the contents of a file unless the file does not exist. If the file does not exist, it will throw an error. You should already know from my previous articles that the standard input is zero, the standard output is one and the standard error is two. With this knowledge in your pocket, the solution looks like this:

    #no more error messages

    cat alfa.txt 2 > /dev/null

    More Miscellaneous Articles
    More By Gabor Bernat


     

    MISCELLANEOUS ARTICLES

    - Using PHP to Stream MP3 Files and Prevent Il...
    - 10 Must Have Firefox Improvements
    - All About OpenOffice 3.0
    - Shell Script Writing
    - Loops in the UNIX Shell
    - The Test in the UNIX Shell
    - Data Streams and the UNIX Shell
    - Control Mechanisms of the UNIX Shell
    - Variables Within the UNIX Shell
    - The Shell and UNIX
    - In Detail: UNIX File Systems
    - Rights Management in UNIX
    - UNIX File Systems
    - The Terminal in UNIX
    - Operating Systems and UNIX





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek