Miscellaneous
  Home arrow Miscellaneous arrow Page 3 - 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 - Redirection and the Here Document


    (Page 3 of 4 )

    If you do some shell programming, eventually you will meet with situations where you want to print both the output and the errors in the same file. The solution takes two steps. Print the content of the output in a file. For this use, I just presented the redirection. Then redirect the standard error to the standard output.

    sort alfa.txt > save.txt 2>&1

    Now, we will not see anything on the terminal. Any information the sort command provides will be printed to the save.txt file. From here, we can read it later if all went well. Because we cannot do anything anyway while the command runs, we may as well just start the command line in the background with the & symbol at the end.

    sort alfa.txt > save.txt 2>&1 &

    Now we can continue our work and see later if the sort was successful. The commands that act like this are logging, and the output file is the log file. You can also use this approach to direct both the error and the output to a further command via the pipe.

    The here document is something that you will use if you write shell programs/scripts. With its help, you can redirect the input of the shell script/program into the file where all of this is. The syntax looks like this:

    command << NAME_OF_DELIMITER

    here you can

    write any kind of text

    what will be the text

    what the script will get as input

    NAME_OF_DELIMITER

    You can use it with any kind of shell command. The command will use as input all the text it can find from the word after the << until it finds the word again. Here the command will get one end of file signal, it will stop, and the shell can continue further with the command on the next line.

    The text between the delimiter words is treated just as if it were surrounded by double quotes. This means that if you include variable names, the shell will replace the variable with its value. You can have any kind of delimiter. However, it is important that in the last line, only that shows, and nothing else. For example, the following script will print out the first argument as the best shell programmer.

    #!/bin/bash

     

    echo "The first argument is $1"

    echo << DELIMITER

    The best shell programmer is $1

    DELIMITER

     

    echo "This is already outside of the here document, Bye bye !"

    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 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek