Miscellaneous

  Home arrow Miscellaneous arrow Page 4 - Data Streams and the UNIX Shell
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

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Data Streams and the UNIX Shell - Saving Streams into Variables


    (Page 4 of 4 )

    Now you can play around with the input and the output as long as they are files. The question is, how can we can save the data stream of a command inside a variable? A variable is a data stream store that lives as long as your program/script. You can save the output of a command to a variable in two ways.

    There is an older, more traditional syntax, and a new one which is more modern. You can use either of them as you wish. The traditional syntax is as follows:

    variable=`command line`

    Here we use the back tick to surround our command line. The modern syntax is just a little different:

    variable=$(command line)

    The result is the same whatever you use. However, the second one is easier to read, so I recommend that you that. For example, we can save the first two lines of a file with the following command:

    #traditional

    firstLines='head -2 alfa.txt'

     

    #modern

    firstLines=$(head -2 alfa.txt)

    The shell will rewrite every new line character to a space in order to fit everything inside the variable and into a single line. The shell will also rewrite the command line before it executes it, if there are any meta-characters. Besides the one presented in my previous article, there are also the following related to the directories:

    *

    It will replace it with the names of the directories inside the working directory

    ~

    The path to the users home directory ( for example /home/gaborj)

    If we want to use them in their literal meaning, you will need to surround it with the forward tick or add a backslash before them. This is all you need to learn for now about streams inside the shell. Rate my articles if you consider that they were worth your attention. Do not hesitate to ask your questions in the blog following the article.

    In the future, we will write more and more scripts inside the shell. Therefore, I need to ask you to make yourself familiar with one of the text editors inside the terminal. If you want something easy to use and learn, I recommend Joe. Nevertheless, if you want to learn to use something more advanced, a tool from which you can benefit in the future, VI Improved is the way to go. You can read a nice article about it here. We will continue next week with some control mechanisms of the shell. Until that arrives, remember to Live With Passion!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.
    blog comments powered by Disqus

    MISCELLANEOUS ARTICLES

    - Oracle Database XE: Indexes and Sequences
    - Modifying Tables in Oracle Database XE
    - Oracle Database XE: Tables and Constraints
    - More on Oracle Databases and Datatypes
    - Oracle Database XE Datatypes: Datetime and L...
    - Oracle Database XE Datatypes: Character and ...
    - From Databases to Datatypes
    - Firefox 3.6.6 Released with Improved Plug-in...
    - Attention Bloggers: WordPress 3.0 Now Releas...
    - Reflection in PHP 5
    - Inheritance and Other Advanced OOP Features
    - Advanced OOP Features
    - Linux from Scratch V.6.6 Review
    - Linux Gaining in Strength
    - Install Slackware on Your Old PC


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 11 - Follow our Sitemap