SEESenv/web/html/chn11Section_5.html
author amit@thunder
Tue, 23 Feb 2010 19:43:04 +0530
changeset 25 ed38dd9bdb50
parent 16 2e3d2cadbcb6
child 27 cb14131583c6
permissions -rw-r--r--
New build of html solving some of the bugs

<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>
   Chapter. Section_
  </title>
  <link rel="stylesheet" href="/review/support/styles.css" type="text/css" />
  <meta name="generator" content="DocBook XSL Stylesheets V1.74.3" />
  <link rel="shortcut icon" type="image/png" href="/review/support/figs/favicon.png" />
  <script type="text/javascript" src="/review/support/jquery-min.js">
  </script>
  <script type="text/javascript" src="/review/support/form.js">
  </script>
  <script type="text/javascript" src="/review/support/hsbook.js">
  </script>
 </head>
 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
  <div class="chapter" id="ch11Section_5">
   <div class="titlepage">
   </div>
   <div class="toc">
    <p>
     <b>
      Table of Contents
     </b>
    </p>
    <dl>
     <dt>
      <span class="article">
       <a href="#id2805047">
       </a>
      </span>
     </dt>
     <dd>
      <dl>
       <dt>
        <span class="section">
         <a href="#id2615138">
          1. Module Objectives:
         </a>
        </span>
       </dt>
       <dt>
        <span class="section">
         <a href="#id2803399">
          2. tar:
         </a>
        </span>
       </dt>
       <dd>
        <dl>
         <dt>
          <span class="section">
           <a href="#id2803952">
            2.1. Introduction:
           </a>
          </span>
         </dt>
         <dt>
          <span class="section">
           <a href="#id2856019">
            2.2. Getting Started(go go go!):
           </a>
          </span>
         </dt>
         <dt>
          <span class="section">
           <a href="#id2856298">
            2.3. Further Reading for this section:
           </a>
          </span>
         </dt>
        </dl>
       </dd>
       <dt>
        <span class="section">
         <a href="#id2856343">
          3. GZip:
         </a>
        </span>
       </dt>
       <dd>
        <dl>
         <dt>
          <span class="section">
           <a href="#id2856368">
            3.1. Get going:
           </a>
          </span>
         </dt>
         <dt>
          <span class="section">
           <a href="#id2856622">
            3.2. Further Reading for this section:
           </a>
          </span>
         </dt>
        </dl>
       </dd>
       <dt>
        <span class="section">
         <a href="#id2856676">
          4. File Comparisons:
         </a>
        </span>
       </dt>
       <dd>
        <dl>
         <dt>
          <span class="section">
           <a href="#id2856693">
            4.1. cmp:
           </a>
          </span>
         </dt>
         <dt>
          <span class="section">
           <a href="#id2856735">
            4.2. diff:
           </a>
          </span>
         </dt>
         <dt>
          <span class="section">
           <a href="#id2856817">
            4.3. comm:
           </a>
          </span>
         </dt>
        </dl>
       </dd>
       <dt>
        <span class="section">
         <a href="#id2856847">
          5. Environment Variables:
         </a>
        </span>
       </dt>
       <dd>
        <dl>
         <dt>
          <span class="section">
           <a href="#id2856946">
            5.1. Further Reading:
           </a>
          </span>
         </dt>
        </dl>
       </dd>
       <dt>
        <span class="section">
         <a href="#id2857001">
          6. Shell Scripting:
         </a>
        </span>
       </dt>
       <dd>
        <dl>
         <dt>
          <span class="section">
           <a href="#id2857009">
            6.1. Basics:
           </a>
          </span>
         </dt>
         <dt>
          <span class="section">
           <a href="#id2855367">
            6.2. Shell Arithmetic:
           </a>
          </span>
         </dt>
         <dt>
          <span class="section">
           <a href="#id2857386">
            6.3. if else construct:
           </a>
          </span>
         </dt>
         <dt>
          <span class="section">
           <a href="#id2857492">
            6.4. Loops
           </a>
          </span>
         </dt>
         <dt>
          <span class="section">
           <a href="#id2857738">
            6.5. Functions
           </a>
          </span>
         </dt>
         <dt>
          <span class="section">
           <a href="#id2857809">
            6.6. Further Reading:
           </a>
          </span>
         </dt>
        </dl>
       </dd>
      </dl>
     </dd>
    </dl>
   </div>
   <div class="article">
    <div class="titlepage">
     <div>
      <div>
       <h2 class="title" id="id2805047">
       </h2>
      </div>
      <div>
       <h2 class="title" id="id2805047">
       </h2>
      </div>
     </div>
     <hr />
    </div>
    <div class="toc">
     <p>
      <b>
       Table of Contents
      </b>
     </p>
     <dl>
      <dt>
       <span class="section">
        <a href="#id2615138">
         1. Module Objectives:
        </a>
       </span>
      </dt>
      <dt>
       <span class="section">
        <a href="#id2803399">
         2. tar:
        </a>
       </span>
      </dt>
      <dd>
       <dl>
        <dt>
         <span class="section">
          <a href="#id2803952">
           2.1. Introduction:
          </a>
         </span>
        </dt>
        <dt>
         <span class="section">
          <a href="#id2856019">
           2.2. Getting Started(go go go!):
          </a>
         </span>
        </dt>
        <dt>
         <span class="section">
          <a href="#id2856298">
           2.3. Further Reading for this section:
          </a>
         </span>
        </dt>
       </dl>
      </dd>
      <dt>
       <span class="section">
        <a href="#id2856343">
         3. GZip:
        </a>
       </span>
      </dt>
      <dd>
       <dl>
        <dt>
         <span class="section">
          <a href="#id2856368">
           3.1. Get going:
          </a>
         </span>
        </dt>
        <dt>
         <span class="section">
          <a href="#id2856622">
           3.2. Further Reading for this section:
          </a>
         </span>
        </dt>
       </dl>
      </dd>
      <dt>
       <span class="section">
        <a href="#id2856676">
         4. File Comparisons:
        </a>
       </span>
      </dt>
      <dd>
       <dl>
        <dt>
         <span class="section">
          <a href="#id2856693">
           4.1. cmp:
          </a>
         </span>
        </dt>
        <dt>
         <span class="section">
          <a href="#id2856735">
           4.2. diff:
          </a>
         </span>
        </dt>
        <dt>
         <span class="section">
          <a href="#id2856817">
           4.3. comm:
          </a>
         </span>
        </dt>
       </dl>
      </dd>
      <dt>
       <span class="section">
        <a href="#id2856847">
         5. Environment Variables:
        </a>
       </span>
      </dt>
      <dd>
       <dl>
        <dt>
         <span class="section">
          <a href="#id2856946">
           5.1. Further Reading:
          </a>
         </span>
        </dt>
       </dl>
      </dd>
      <dt>
       <span class="section">
        <a href="#id2857001">
         6. Shell Scripting:
        </a>
       </span>
      </dt>
      <dd>
       <dl>
        <dt>
         <span class="section">
          <a href="#id2857009">
           6.1. Basics:
          </a>
         </span>
        </dt>
        <dt>
         <span class="section">
          <a href="#id2855367">
           6.2. Shell Arithmetic:
          </a>
         </span>
        </dt>
        <dt>
         <span class="section">
          <a href="#id2857386">
           6.3. if else construct:
          </a>
         </span>
        </dt>
        <dt>
         <span class="section">
          <a href="#id2857492">
           6.4. Loops
          </a>
         </span>
        </dt>
        <dt>
         <span class="section">
          <a href="#id2857738">
           6.5. Functions
          </a>
         </span>
        </dt>
        <dt>
         <span class="section">
          <a href="#id2857809">
           6.6. Further Reading:
          </a>
         </span>
        </dt>
       </dl>
      </dd>
     </dl>
    </div>
    <div class="section" title="1.Module Objectives:">
     <div class="titlepage">
      <div>
       <div>
        <h2 class="title" style="clear: both">
         <a name="id2615138">
         </a>
         1.Module Objectives:
        </h2>
       </div>
      </div>
     </div>
     <p id="ch11Section_5_1">
      After successfully completing this module a participant will be able to:
     </p>
     <pre class="programlisting">
      - Understand
  * What are archives and zipped files                              U
  * What are environment variables                                  U
  * What are Shell Scripts                                          U
- Able to use file comparison commands like                         Ap
  diff, cmp, comm
- Create and extract archives(.tar files) and zipped files(.gz)     Ap
- Set/Modify environment as per need                                Ap
- Create shell scripts to automate tasks.                           Ap
     </pre>
    </div>
    <div class="section" title="2.tar:">
     <div class="titlepage">
      <div>
       <div>
        <h2 class="title" style="clear: both">
         <a name="id2803399">
         </a>
         2.tar:
        </h2>
       </div>
      </div>
     </div>
     <div class="section" title="2.1.Introduction:">
      <div class="titlepage">
       <div>
        <div>
         <h3 class="title">
          <a name="id2803952">
          </a>
          2.1.Introduction:
         </h3>
        </div>
       </div>
      </div>
      <p id="ch11Section_5_2">
       In world of Linux based distribution,
       <span class="emphasis">
        <em>
         tarballs
        </em>
       </span>
       is the term which pops up very often. It is part of the GNU project and comes as part of every distribution of GNU/Linux. Tarball is like defacto standard for releasing source code for free software. Some of common use of
       <span class="emphasis">
        <em>
         tar
        </em>
       </span>
       archives is to:
       <span class="emphasis">
        <em>
         Store, backup, and transport
        </em>
       </span>
       .
      </p>
      <p id="ch11Section_5_3">
       GNU tar creates and manipulates archives which are actually collections of many other files; the program provides users with an organized and systematic method for controlling a large amount of data. It is basically form of creating archive by concatenating one or more files.
      </p>
     </div>
     <div class="section" title="2.2.Getting Started(go go go!):">
      <div class="titlepage">
       <div>
        <div>
         <h3 class="title">
          <a name="id2856019">
          </a>
          2.2.Getting Started(go go go!):
         </h3>
        </div>
       </div>
      </div>
      <p id="ch11Section_5_4">
       As mentioned previously and if not,
       <span class="emphasis">
        <em>
         The best way to get started with any command line tool of Linux is to use "man".
        </em>
       </span>
      </p>
      <pre class="programlisting">
       $ man tar
      </pre>
      <p id="ch11Section_5_5">
       or try these commands(the output may vary with different installations):
      </p>
      <pre class="programlisting">
       $ tar --version
tar (GNU tar) 1.20
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later &lt;http://gnu.org/licenses/gpl.html&gt;
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by John Gilmore and Jay Fenlason.

$ tar --help
Usage: tar [OPTION...] [FILE]...
GNU `tar' saves many files together into a single tape or disk archive, and can
restore individual files from the archive.
Examples:
tar -cf archive.tar foo bar  # Create archive.tar from files foo and bar.
tar -tvf archive.tar         # List all files in archive.tar verbosely.
tar -xf archive.tar          # Extract all files from archive.tar.
____________
      </pre>
      <div class="section" title="2.2.1.Creating a tar archive:">
       <div class="titlepage">
        <div>
         <div>
          <h4 class="title">
           <a name="id2856073">
           </a>
           2.2.1.Creating a tar archive:
          </h4>
         </div>
        </div>
       </div>
       <p id="ch11Section_5_6">
        We will do some off-the road activity for this exercise. We will use an interesting command
        <span class="emphasis">
         <em>
          fortune
         </em>
        </span>
        for creating our practice files and then performing archives of those files and directories. Content of the files would vary for users, as fortune works like that.
       </p>
       <pre class="programlisting">
        $ mkdir fortune-files
$ cd fortune-files/
$ fortune &gt; first.txt
$ cat first.txt
Expect the worst, it's the least you can do.
$ fortune &gt; second.txt
$ fortune &gt; third.txt
$ ls
first.txt  second.txt  third.txt
       </pre>
       <p id="ch11Section_5_7">
        By now we have three txt files, with some random fortune content. To create a tar archive of these files we can use any of following commands according to ones convenience:
       </p>
       <pre class="programlisting">
        $ tar --create --verbose --file=allfiles.tar first.txt second.txt third.txt
first.txt
second.txt
third.txt
$ ls
allfiles.tar  first.txt  second.txt  third.txt
       </pre>
       <p id="ch11Section_5_8">
        allfiles.tar is our required tar archive of all the rest of files(or archive of files mentioned in command line). Other form of the previous command are:
       </p>
       <pre class="programlisting">
        $ tar -c -v -f allfiles.tar first.txt second.txt third.txt
       </pre>
       <p id="ch11Section_5_9">
        or
       </p>
       <pre class="programlisting">
        $ tar -cvf allfiles.tar first.txt second.txt third.txt
       </pre>
       <p id="ch11Section_5_a">
        The general format for creating a tar archive is:
       </p>
       <pre class="programlisting">
        tar [OPTION...] [FILE]...
       </pre>
       <p id="ch11Section_5_b">
        For our command are using these options:
       </p>
       <div class="itemizedlist">
        <ul class="itemizedlist" type="*">
         <li class="listitem" style="list-style-type: *">
          <p id="ch11Section_5_c">
           -c to Create the archive.
          </p>
         </li>
         <li class="listitem" style="list-style-type: *">
          <p id="ch11Section_5_d">
           -v for Verbose mode, to get the names of the files as they are archived.
          </p>
         </li>
         <li class="listitem" style="list-style-type: *">
          <p id="ch11Section_5_e">
           -f mentioning the file name of the resulting tar archive.
          </p>
         </li>
        </ul>
       </div>
       <p id="ch11Section_5_f">
        To create archive of folder itself try this:
       </p>
       <pre class="programlisting">
        $ tar -cvf fortune.tar fortune/
       </pre>
       <p id="ch11Section_5_10">
        To add files to existing tar archive, option
        <span class="emphasis">
         <em>
          `r`
         </em>
        </span>
        is used:
       </p>
       <pre class="programlisting">
        $ fortune &gt; fourth.txt
$ tar -r fourth.txt -vf allfiles.tar
fourth.txt
       </pre>
       <p id="ch11Section_5_11">
        There are other options too available for explicitly mentioning the position of archive, use
        <span class="emphasis">
         <em>
          tar --help
         </em>
        </span>
        for getting all the details.
       </p>
       <p id="ch11Section_5_12">
        Similarly to remove file from archive use
        <span class="emphasis">
         <em>
          --delete
         </em>
        </span>
        option:
       </p>
       <pre class="programlisting">
        $ tar --delete second.txt -f allfiles.tar
$ tar -tf allfiles.tar
first.txt
third.txt
fourth.txt
       </pre>
      </div>
      <div class="section" title="2.2.2.Listing the files of archive:">
       <div class="titlepage">
        <div>
         <div>
          <h4 class="title">
           <a name="id2856213">
           </a>
           2.2.2.Listing the files of archive:
          </h4>
         </div>
        </div>
       </div>
       <p id="ch11Section_5_13">
        Once files are archived, tar command have the
        <span class="emphasis">
         <em>
          `t`
         </em>
        </span>
        option, for Listing all files in the tar file:
       </p>
       <pre class="programlisting">
        $ tar tf allfiles.tar
first.txt
second.txt
third.txt
       </pre>
       <p id="ch11Section_5_14">
        <span class="strong">
         <strong>
          //this is not working for me in some cases :(
         </strong>
        </span>
       </p>
       <p id="ch11Section_5_15">
        To locate a particular file among the archive mention its name after
        <span class="emphasis">
         <em>
          t
         </em>
        </span>
        option.
       </p>
       <pre class="programlisting">
        $ tar t second.txt allfiles.tar
second.txt
       </pre>
       <p id="ch11Section_5_16">
        one can also use elementary regex for locating the file, so in previous case even second.* will also return the same result.
       </p>
      </div>
      <div class="section" title="2.2.3.Extracting files from archive:">
       <div class="titlepage">
        <div>
         <div>
          <h4 class="title">
           <a name="id2856263">
           </a>
           2.2.3.Extracting files from archive:
          </h4>
         </div>
        </div>
       </div>
       <p id="ch11Section_5_17">
        To extract the content from a archive, use
        <span class="emphasis">
         <em>
          `x`
         </em>
        </span>
        option:
       </p>
       <pre class="programlisting">
        $ mkdir extract
$ cp allfiles.tar extract/
$ cd extract
$ tar -xvf allfiles.tar
first.txt
second.txt
third.txt
       </pre>
       <p id="ch11Section_5_18">
        To extract any particular file from archive, mention the name of file after
        <span class="emphasis">
         <em>
          x
         </em>
        </span>
        option:
       </p>
       <pre class="programlisting">
        $ tar -x second.txt -vf allfiles.tar
second.txt
       </pre>
      </div>
     </div>
     <div class="section" title="2.3.Further Reading for this section:">
      <div class="titlepage">
       <div>
        <div>
         <h3 class="title">
          <a name="id2856298">
          </a>
          2.3.Further Reading for this section:
         </h3>
        </div>
       </div>
      </div>
      <div class="itemizedlist">
       <ul class="itemizedlist" type="*">
        <li class="listitem" style="list-style-type: *">
         <p id="ch11Section_5_19">
         </p>
         <div class="reference">
          <div class="titlepage">
           <hr />
          </div>
          http://en.wikipedia.org/wiki/Tar_(file_format
         </div>
         <p>
          )
         </p>
        </li>
        <li class="listitem" style="list-style-type: *">
         <p id="ch11Section_5_1a">
         </p>
         <div class="reference">
          <div class="titlepage">
           <hr />
          </div>
          http://www.gnu.org/software/tar/manual/tar.html
         </div>
        </li>
        <li class="listitem" style="list-style-type: *">
         <p id="ch11Section_5_1b">
         </p>
         <div class="reference">
          <div class="titlepage">
           <hr />
          </div>
          http://linuxreviews.org/beginner/
         </div>
        </li>
       </ul>
      </div>
     </div>
    </div>
    <div class="section" title="3.GZip:">
     <div class="titlepage">
      <div>
       <div>
        <h2 class="title" style="clear: both">
         <a name="id2856343">
         </a>
         3.GZip:
        </h2>
       </div>
      </div>
     </div>
     <p id="ch11Section_5_1c">
      Tar creates archives but it does not compress data by itself unless specified explicitly. Hence all the archive we create using tar command, is simply of the size of total size of all individual files. With Linux there is a compression tool known as
      <span class="emphasis">
       <em>
        gzip
       </em>
      </span>
      which is used to reduce the size of files mentioned. Whenever possible, each file is replaced by one with the extension .gz', so unlike `tar this command would
      <span class="emphasis">
       <em>
        replace the existing file
       </em>
      </span>
      .
     </p>
     <div class="section" title="3.1.Get going:">
      <div class="titlepage">
       <div>
        <div>
         <h3 class="title">
          <a name="id2856368">
          </a>
          3.1.Get going:
         </h3>
        </div>
       </div>
      </div>
      <p id="ch11Section_5_1d">
       As usual first commands to check out with gzip are
       <span class="emphasis">
        <em>
         man
        </em>
       </span>
       and
       <span class="emphasis">
        <em>
         help
        </em>
       </span>
       ,
      </p>
      <pre class="programlisting">
       $ man gzip
$ gzip --help
      </pre>
      <p id="ch11Section_5_1e">
       Creating a zip of a bunch of files is fairly trivial, it can be done simply via:
      </p>
      <pre class="programlisting">
       $ gzip [OPTION]... [FILE]...
      </pre>
      <div class="section" title="3.1.1.Creating zip files:">
       <div class="titlepage">
        <div>
         <div>
          <h4 class="title">
           <a name="id2856399">
           </a>
           3.1.1.Creating zip files:
          </h4>
         </div>
        </div>
       </div>
       <p id="ch11Section_5_1f">
        Continuing from previous set of files and setup, we will like to zip them and hence the command would be:
       </p>
       <pre class="programlisting">
        $ gzip first.txt fourth.txt second.txt third.txt
$ ls
allfiles.tar  first.txt.gz  fourth.txt.gz  second.txt.gz  third.txt.gz  zipped.tar.gz
       </pre>
       <p id="ch11Section_5_20">
        Hence, as mentioned above, all original files are replaced by .gz extension. The above task can also be restated and made easy with help of some common regex expression:
       </p>
       <pre class="programlisting">
        $ gzip *.txt
       </pre>
       <p id="ch11Section_5_21">
        Similar to
        <span class="emphasis">
         <em>
          tar
         </em>
        </span>
        command, one can also use
        <span class="emphasis">
         <em>
          `-v`
         </em>
        </span>
        option here to see the output in
        <span class="emphasis">
         <em>
          verbose
         </em>
        </span>
        form. For the previous example, if we enable this option the result would be something like this:
       </p>
       <pre class="programlisting">
        $ gzip -v *.txt
first.txt:    4.4% -- replaced with first.txt.gz
fourth.txt:  -7.1% -- replaced with fourth.txt.gz
second.txt:  -4.8% -- replaced with second.txt.gz
third.txt:    3.8% -- replaced with third.txt.gz
       </pre>
       <p id="ch11Section_5_22">
        For files of very small sizes and some other cases, one might end up with a zipped file whose size is greater then original file, but compression is always performed(so don't be disheartened in the above case, as files are larger :P). So unlike tar, here all files are zipped separately by default, to make them part of one single chunk one can use some
        <span class="emphasis">
         <em>
          pipes
         </em>
        </span>
        and
        <span class="emphasis">
         <em>
          redirections
         </em>
        </span>
       </p>
       <pre class="programlisting">
        $ gzip -c *.txt &gt; all.gz
       </pre>
       <p id="ch11Section_5_23">
        Now in this case, all files would be zipped, concatenated and then the output would be written to a file all.gz leaving back all the original files. In the command above
        <span class="emphasis">
         <em>
          `-c`
         </em>
        </span>
        option states to print the output to standard output(stdout) and following
        <span class="emphasis">
         <em>
          `&gt;`
         </em>
        </span>
        would redirect the output to file all.gz. So when we decompress this file, we will get a single file named 'all' with all the content of each files concatenated one after the another.
       </p>
       <p id="ch11Section_5_24">
        For creating a zip archive of a complete directory, one has to use
        <span class="emphasis">
         <em>
          `-r`
         </em>
        </span>
        options which means recursive, it makes gzip to traverse through all directory tree/structure. By default it will create zip files of each file inside the directory, that is even with the -r flag, gzip still compresses one file at a time :
       </p>
       <pre class="programlisting">
        $ gzip -r fortune-files/
$ gzip -rv .
./first.txt:          4.4% -- replaced with ./first.txt.gz
./second.txt:        -4.8% -- replaced with ./second.txt.gz
./third.txt:          3.8% -- replaced with ./third.txt.gz
./allfiles.tar:      96.6% -- replaced with ./allfiles.tar.gz
./fourth.txt:        -7.1% -- replaced with ./fourth.txt.gz
       </pre>
       <p id="ch11Section_5_25">
        Hence one always sees files like xxxxx.tar.gz, to create a zip of whole directory in a single file, first archive everything inside a folder and then use gzip on that. For zipping the files using tar itself, one has to use the option
        <span class="emphasis">
         <em>
          `g`
         </em>
        </span>
        .
       </p>
       <pre class="programlisting">
        $ tar -cvzf zipped.tar.gz *.txt
first.txt
fourth.txt
second.txt
third.txt
       </pre>
       <p id="ch11Section_5_26">
        <span class="emphasis">
         <em>
          Thats why gzip is designed as a complement to tar, not as a replacement.
         </em>
        </span>
       </p>
       <p id="ch11Section_5_27">
        gzip command comes with a option
        <span class="emphasis">
         <em>
          `-l`
         </em>
        </span>
        to view the compressed file contents:
       </p>
       <pre class="programlisting">
        $ gzip -l zipped.tar.gz
         compressed        uncompressed  ratio uncompressed_name
            332               10240      97.0% zipped.tar
       </pre>
       <p id="ch11Section_5_28">
        Other feature of gzip is option for mentioning the kind of compression one wants. There is a option of
        <span class="emphasis">
         <em>
          `-n`
         </em>
        </span>
        where
        <span class="emphasis">
         <em>
          n varies from 0 to 9
         </em>
        </span>
        which regulate the speed/quality of compression. With
        <span class="emphasis">
         <em>
          `-1`
         </em>
        </span>
        or
        <span class="emphasis">
         <em>
          `--fast`
         </em>
        </span>
        option it means the fastest compression method (less compression) and
        <span class="emphasis">
         <em>
          `--best`
         </em>
        </span>
        or
        <span class="emphasis">
         <em>
          `-9`
         </em>
        </span>
        indicates the slowest compression method, default compression level is
        <span class="emphasis">
         <em>
          `-6`
         </em>
        </span>
        .
       </p>
       <p id="ch11Section_5_29">
        To decompress a already compressed file there are two options, either use
        <span class="emphasis">
         <em>
          `gunzip`
         </em>
        </span>
        command or use
        <span class="emphasis">
         <em>
          `-d`
         </em>
        </span>
        option with gzip command:
       </p>
       <pre class="programlisting">
        $ gzip -dv *.gz
all.gz:     -440.4% -- replaced with all
first.txt.gz:         4.4% -- replaced with first.txt
fourth.txt.gz:       -7.1% -- replaced with fourth.txt
second.txt.gz:       -4.8% -- replaced with second.txt
third.txt.gz:         3.8% -- replaced with third.txt
zipped.tar.gz:       97.0% -- replaced with zipped.tar
       </pre>
       <p id="ch11Section_5_2a">
        or:
       </p>
       <pre class="programlisting">
        $ gunzip -v *.gz
       </pre>
       <p id="ch11Section_5_2b">
        Both of those commands will give the same result. So here one can notice the content of file "all" which we created earlier, it will have content of all the rest of four files concatenated one after another, but "zipped.tar.gz" is zip of tar of all files, will effectively have zip of archives of all files separately, and hence the usage and importance of
        <span class="emphasis">
         <em>
          tar
         </em>
        </span>
        .
       </p>
      </div>
     </div>
     <div class="section" title="3.2.Further Reading for this section:">
      <div class="titlepage">
       <div>
        <div>
         <h3 class="title">
          <a name="id2856622">
          </a>
          3.2.Further Reading for this section:
         </h3>
        </div>
       </div>
      </div>
      <div class="itemizedlist">
       <ul class="itemizedlist" type="*">
        <li class="listitem" style="list-style-type: *">
         <p id="ch11Section_5_2c">
         </p>
         <div class="reference">
          <div class="titlepage">
           <hr />
          </div>
          http://linuxreviews.org/beginner/
         </div>
        </li>
        <li class="listitem" style="list-style-type: *">
         <p id="ch11Section_5_2d">
         </p>
         <div class="reference">
          <div class="titlepage">
           <hr />
          </div>
          http://lowfatlinux.com/linux-gzip-gunzip.html
         </div>
        </li>
        <li class="listitem" style="list-style-type: *">
         <p id="ch11Section_5_2e">
         </p>
         <div class="reference">
          <div class="titlepage">
           <hr />
          </div>
          http://www.gnu.org/software/gzip/manual/gzip.html
         </div>
        </li>
        <li class="listitem" style="list-style-type: *">
         <p id="ch11Section_5_2f">
         </p>
         <div class="reference">
          <div class="titlepage">
           <hr />
          </div>
          http://en.wikipedia.org/wiki/ZIP_(file_format
         </div>
         <p>
          )
         </p>
        </li>
       </ul>
      </div>
     </div>
    </div>
    <div class="section" title="4.File Comparisons:">
     <div class="titlepage">
      <div>
       <div>
        <h2 class="title" style="clear: both">
         <a name="id2856676">
         </a>
         4.File Comparisons:
        </h2>
       </div>
      </div>
     </div>
     <p id="ch11Section_5_30">
      Linux based distributions also have some utilities for checking the content of files, comparing them very quickly to other files. These operations can be looking for differences/similarities. Some of the commands which prove handy are:
     </p>
     <div class="section" title="4.1.cmp:">
      <div class="titlepage">
       <div>
        <div>
         <h3 class="title">
          <a name="id2856693">
          </a>
          4.1.cmp:
         </h3>
        </div>
       </div>
      </div>
      <p id="ch11Section_5_31">
       If one wants to compare two files whether they are same or not, one can use this handy tool. Let us consider some situation, we run find/locate command to locate some file, and it turns out that we have a file with same name in different location, and in case we want to run a quick check on there content, cmp is the right tool. For my system I perform these tasks to illustrate the use of this command:
      </p>
      <pre class="programlisting">
       $ find . -name quick.c
./Desktop/programs/quick.c
./c-folder/quick.c
$ cmp Desktop/programs/quick.c c-folder/quick.c
$
      </pre>
      <p id="ch11Section_5_32">
       For me it returns nothing, hence that means both the files are exact copy of each other, by default, cmp is silent if the files are the same. Make some changes in one of the file and rerun the command. For me it works like this:
      </p>
      <pre class="programlisting">
       $ cmp Desktop/programs/quick.c c-folder/quick.c
Desktop/programs/quick.c c-folder/quick.c differ: byte 339, line 24
      </pre>
      <p id="ch11Section_5_33">
       That is, if files differ, the byte and line number at which the first difference occurred is reported.
      </p>
     </div>
     <div class="section" title="4.2.diff:">
      <div class="titlepage">
       <div>
        <div>
         <h3 class="title">
          <a name="id2856735">
          </a>
          4.2.diff:
         </h3>
        </div>
       </div>
      </div>
      <p id="ch11Section_5_34">
       Now there are situations when one wants to exactly know the differences among two files, for them, GNU diff can show whether files are different without detailing the differences. For simple and basic usage of this programs, consider following example:
      </p>
      <pre class="programlisting">
       $ echo -e "quick nbrown nfox njumped nover nthe nlazy ndog" &gt; allcharacters.txt
$ echo -e "quick nbrown nfox njmuped nover nteh nlzay ndog" &gt; problem.txt
$ diff problem.txt allcharacters.txt
4c4
&lt; jmuped
---
&gt; jumped
6,7c6,7
&lt; teh
&lt; lzay
---
&gt; the
&gt; lazy
      </pre>
      <p id="ch11Section_5_35">
       Looking at results above mentioned it is very trivial to deduce that, diff if used on two separate text files will result in line by line results for all the lines which are different. So most common use case scenario can be, got some files in various location of system with same name and size, just run diff through them and remove all the redundant files. Other similar command which one can find more effective for this can be
       <span class="emphasis">
        <em>
         sdiff
        </em>
       </span>
       , for the same files using sdiff will result in:
      </p>
      <pre class="programlisting">
       $ sdiff problem.txt allcharacters.txt
quick                                                               quick
brown                                                               brown
fox                                                                 fox
jmuped                                                            | jumped
over                                                                over
teh                                                               | the
lzay                                                              | lazy
dog                                                                 dog
      </pre>
      <p id="ch11Section_5_36">
       Some exercise for a change:
      </p>
      <div class="itemizedlist">
       <ul class="itemizedlist" type="*">
        <li class="listitem" style="list-style-type: *">
         <p id="ch11Section_5_37">
          Try using diff for any binary file, does it work?
         </p>
        </li>
        <li class="listitem" style="list-style-type: *">
         <p id="ch11Section_5_38">
          What are other equivalent for diff command based on needs/requirements?
         </p>
        </li>
        <li class="listitem" style="list-style-type: *">
         <p id="ch11Section_5_39">
          Can we use diff to compare two directories? If yes how?
         </p>
        </li>
       </ul>
      </div>
     </div>
     <div class="section" title="4.3.comm:">
      <div class="titlepage">
       <div>
        <div>
         <h3 class="title">
          <a name="id2856817">
          </a>
          4.3.comm:
         </h3>
        </div>
       </div>
      </div>
      <p id="ch11Section_5_3a">
       This is one more command which proves handy at times, the short and sweet man page states "compare two sorted files line by line". Or this it compares sorted files and selects or rejects lines common to two files. For ex:
      </p>
      <pre class="programlisting">
       $ sort allcharacters.txt&gt;sortedcharac.txt; sort problem.txt&gt;sortedprob.txt
$ comm sortedcharac.txt sortedprob.txt
             brown
             dog
             fox
     jmuped
jumped
lazy
     lzay
             over
             quick
     teh
the
      </pre>
     </div>
    </div>
    <div class="section" title="5.Environment Variables:">
     <div class="titlepage">
      <div>
       <div>
        <h2 class="title" style="clear: both">
         <a name="id2856847">
         </a>
         5.Environment Variables:
        </h2>
       </div>
      </div>
     </div>
     <p id="ch11Section_5_3b">
      These variables like HOME, OSTYPE,Variables are a way of passing information from the shell to programs when you run them. Programs look "in the environment" for particular variables and if they are found will use the values stored. Standard UNIX variables are split into two categories, environment variables and shell variables. In broad terms, shell variables apply only to the current instance of the shell and are used to set short-term working conditions; environment variables have a farther reaching significance, and those set at login are valid for the duration of the session.By convention, environment variables have UPPER CASE and shell variables have lower case names.
     </p>
     <p id="ch11Section_5_3c">
      Some of examples of Environment variables are(result may vary!):
     </p>
     <pre class="programlisting">
      $ echo $OSTYPE
linux-gnu
$ echo $HOME
/home/baali
     </pre>
     <p id="ch11Section_5_3d">
      To see all the variables and there values use any of following commands:
     </p>
     <pre class="programlisting">
      $ printenv | less
$ env
     </pre>
     <p id="ch11Section_5_3e">
      The most commonly used environment variable is "PATH", it defines a list of directories to search through when looking for a command to execute. If you decide to put your own programs in a bin directory under your home directory, you'll have to modify the path to include that directory, or the system will never find your programs (unless you happen to be in that directory when you enter the command). Here's how to change your PATH variable so it includes your personal bin directory:
     </p>
     <pre class="programlisting">
      $ set PATH=$PATH:$HOME/bin
     </pre>
     <p id="ch11Section_5_3f">
      See the difference in value of PATH variable before and after modifying it. One can also create its own variable to make things easier:
     </p>
     <pre class="programlisting">
      $ set repo = $HOME/Desktop/random/code
$ cd $repo
     </pre>
     <p id="ch11Section_5_40">
      <span class="emphasis">
       <em>
        set
       </em>
      </span>
      command is used to define a variable for the current shell. Try opening a new shell and use the above mentioned command, it wont work as expected. The other child process wont be able to see these variables unless we
      <span class="emphasis">
       <em>
        export
       </em>
      </span>
      them. Repeat the above mentioned activity with
      <span class="emphasis">
       <em>
        export
       </em>
      </span>
      command. Now with all new shells,
      <span class="emphasis">
       <em>
        $repo
       </em>
      </span>
      will work.
     </p>
     <p id="ch11Section_5_41">
      Again these changes are limited to current session. To make them permanent or get loaded each time you log in, just add those lines to
      <span class="emphasis">
       <em>
        .bashrc
       </em>
      </span>
      file.
     </p>
     <div class="section" title="5.1.Further Reading:">
      <div class="titlepage">
       <div>
        <div>
         <h3 class="title">
          <a name="id2856946">
          </a>
          5.1.Further Reading:
         </h3>
        </div>
       </div>
      </div>
      <div class="itemizedlist">
       <ul class="itemizedlist" type="*">
        <li class="listitem" style="list-style-type: *">
         <p id="ch11Section_5_42">
         </p>
         <div class="reference">
          <div class="titlepage">
           <hr />
          </div>
          http://lowfatlinux.com/linux-environment-variables.html
         </div>
        </li>
        <li class="listitem" style="list-style-type: *">
         <p id="ch11Section_5_43">
         </p>
         <div class="reference">
          <div class="titlepage">
           <hr />
          </div>
          http://www.codecoffee.com/tipsforlinux/articles/030.html
         </div>
        </li>
        <li class="listitem" style="list-style-type: *">
         <p id="ch11Section_5_44">
         </p>
         <div class="reference">
          <div class="titlepage">
           <hr />
          </div>
          http://www.ee.surrey.ac.uk/Teaching/Unix/unix8.html
         </div>
        </li>
        <li class="listitem" style="list-style-type: *">
         <p id="ch11Section_5_45">
         </p>
         <div class="reference">
          <div class="titlepage">
           <hr />
          </div>
          http://en.wikipedia.org/wiki/Environment_variable
         </div>
        </li>
       </ul>
      </div>
     </div>
    </div>
    <div class="section" title="6.Shell Scripting:">
     <div class="titlepage">
      <div>
       <div>
        <h2 class="title" style="clear: both">
         <a name="id2857001">
         </a>
         6.Shell Scripting:
        </h2>
       </div>
      </div>
     </div>
     <div class="section" title="6.1.Basics:">
      <div class="titlepage">
       <div>
        <div>
         <h3 class="title">
          <a name="id2857009">
          </a>
          6.1.Basics:
         </h3>
        </div>
       </div>
      </div>
      <p id="ch11Section_5_46">
       Shell program or shell script,a sequence of commands to a text file and tell the shell to execute the text file instead of entering the commands. The first
       <span class="emphasis">
        <em>
         "Hello World"
        </em>
       </span>
       sample for shell scripting is as easy as it sounds:
      </p>
      <pre class="programlisting">
       $ echo '#!/bin/sh' &gt; my-script.sh
$ clear &gt;&gt; my-script.sh
$ echo 'echo Hello World' &gt;&gt; my-script.sh
$ chmod 755 my-script.sh
$ ./my-script.sh
Hello World
      </pre>
      <p id="ch11Section_5_47">
       The #! syntax(also known as shebang) is used in scripts to indicate an interpreter for execution under UNIX / Linux operating systems. The chmod is required to make the script executable. This script will just execute two commands,
       <span class="emphasis">
        <em>
         clear
        </em>
       </span>
       and
       <span class="emphasis">
        <em>
         echo
        </em>
       </span>
       one after another. One can also do the same task using a one liner command
       <span class="emphasis">
        <em>
         clear; echo 'Hello World';
        </em>
       </span>
       but as number of lines grows using a script file is helpful.
      </p>
      <p id="ch11Section_5_48">
       So lets create a script which gives us all the filenames for given initial alphabet or string in a directory. Let the name of script be
       <span class="emphasis">
        <em>
         initial.sh
        </em>
       </span>
       , open it with text editor, and write:
      </p>
      <pre class="programlisting">
       #!/bin/sh
ls &gt; temp
grep ^$1 &lt; temp
rm temp
$ chmod a+x initial.sh
$ ./initial.sh s
      </pre>
      <p id="ch11Section_5_49">
       The $1 in the script is pertaining to command line argument. All arguments passed via command line are accessed via
       <span class="emphasis">
        <em>
         $#
        </em>
       </span>
       with name of script being first member, that is $0. Now lets write a script for finding a file, and then checking when was it last modified:
      </p>
      <pre class="programlisting">
       #!/bin/sh
name=`find . -name $1 -print`
echo $name
last_modified=`stat -c %y $name| cut -f 1 -d " "`
echo "Last modified: $last_modified"
$ ./search.sh fname
      </pre>
      <p id="ch11Section_5_4a">
       Try giving some file you want to search in place of fname. Please note in second line
       <span class="emphasis">
        <em>
         `
        </em>
       </span>
       its a back-quote(other key mapped with tilda), it is specifically used to get the output of one command into a variable. In this particular case name is a User defined variables (UDV) which stores the value. We access value stored in any variable using
       <span class="emphasis">
        <em>
         $
        </em>
       </span>
       symbol before name of variable.
      </p>
      <p id="ch11Section_5_4b">
       naming conventions for variables?? do we need them??
      </p>
     </div>
     <div class="section" title="6.2.Shell Arithmetic:">
      <div class="titlepage">
       <div>
        <div>
         <h3 class="title">
          <a name="id2855367">
          </a>
          6.2.Shell Arithmetic:
         </h3>
        </div>
       </div>
      </div>
      <p id="ch11Section_5_4c">
       Shell also provides support for basic arithmetic operations. The syntax is:
      </p>
      <pre class="programlisting">
       $ expr op1 math-operator op2
      </pre>
      <p id="ch11Section_5_4d">
       Some of example which can be tried handily:
      </p>
      <pre class="programlisting">
       $ expr -3 + 5
2
$ expr 10 % 3
1
      </pre>
      <p id="ch11Section_5_4e">
       These spaces in between operator and operands is important, without them shell interpreter will raise the syntax error.
      </p>
      <pre class="programlisting">
       $ expr 2*3
expr: syntax error
      </pre>
      <p id="ch11Section_5_4f">
       One can use back-quotes(`) also to get value of expr.
      </p>
      <pre class="programlisting">
       $ echo `expr 6 + 3`
9
$ result=`expr 6 + 3`
$ echo $result
9
      </pre>
      <p id="ch11Section_5_50">
       Shell uses three kinds of quotes. Double quotes("), anything enclosed among them except from variable trailing after $, and characters after would be printed as it is. Single quotes('), anything enclosed within them is just same, no formulation/interpretation. Back quotes(`), anything inclosed is considered as command, or is executed.
      </p>
      <pre class="programlisting">
       $ echo "Today is date"
Today is date
$ echo "Today is `date`"
Today is Wed Sep 16 17:32:22 IST 2009
$ echo 'Today is `date`'
Today is `date`
$ echo "Today is  n `date`"
Today is  n Wed Sep 16 17:40:13 IST 2009
$ echo -e "Today is  n `date`"
Today is
 Wed Sep 16 17:41:13 IST 2009
      </pre>
     </div>
     <div class="section" title="6.3.if else construct:">
      <div class="titlepage">
       <div>
        <div>
         <h3 class="title">
          <a name="id2857386">
          </a>
          6.3.if else construct:
         </h3>
        </div>
       </div>
      </div>
      <p id="ch11Section_5_51">
       One can have simple
       <span class="emphasis">
        <em>
         if else if
        </em>
       </span>
       constructs in shell scripts to check conditions. Lets take simple example of writing a script which returns back whether the argument passed is positive or not:
      </p>
      <pre class="programlisting">
       #!/bin/sh
if test $1 -gt 0
then
  echo "number is positive"
else
  echo "number is negative"
fi
$ ./sign.sh -11
number is negative
      </pre>
      <p id="ch11Section_5_52">
       This script will compare the first value passed as argument with 0
       <span class="emphasis">
        <em>
         if test var -gt val
        </em>
       </span>
       , var being $1 and val being 0, gt meaning greater then. Now this program has some flaw, it will give same result for following input: (-11) and (-1, 5), as we are checking just $1 which is first argument and hence the result. For handling such situation we can include
       <span class="emphasis">
        <em>
         if-else
        </em>
       </span>
       clause which will warn user of correct usage of script.
      </p>
      <pre class="programlisting">
       #this is the case when no argument is passed
if [ $# -eq 0 ]
then
  echo "$0 : You must give/supply one integers"
  exit 1
else
  if [ $# -gt 1 ]
  then
    echo "$0 : You must give one integer"
    exit 1
  fi
fi
      </pre>
      <p id="ch11Section_5_53">
       One important thing to not in shell script is spacing, with many comparison and evaluation operation a wrongly placed space will spoil all the fun. So in previous example the expression
       <span class="emphasis">
        <em>
         [ $# -eq 0 ]
        </em>
       </span>
       will work properly, but if we remove those leading or trailing spaces like
       <span class="emphasis">
        <em>
         [ $# -eq 0]
        </em>
       </span>
       , it wont work as expected, or rather throw a warning. Both
       <span class="emphasis">
        <em>
         test
        </em>
       </span>
       and
       <span class="emphasis">
        <em>
         []
        </em>
       </span>
       do the same task of testing a expression and returning true or false.
      </p>
      <p id="ch11Section_5_54">
       Lets create something interesting using these if-else clause. Now we will create a script which will greet the user when he opens the shell. We will create the script, change the permission to make it executable and append the
       <span class="emphasis">
        <em>
         .bashrc
        </em>
       </span>
       file with
       <span class="emphasis">
        <em>
         ./greet.sh
        </em>
       </span>
       line and we are done. The script is:
      </p>
      <pre class="programlisting">
       #!/bin/sh
#Script to greet the user according to time of day
temph=`date | cut -c12-13`
dat=`date +"%A %d in %B of %Y (%r)"`
if [ $temph -lt 12 ]
then
  mess="Good Morning $LOGNAME, Have a nice day!"
fi

if [ $temph -gt 12 -a $temph -le 16 ]
then
  mess="Good Afternoon $LOGNAME"
fi

if [ $temph -gt 16 -a $temph -le 18 ]
then
  mess="Good Evening $LOGNAME"
fi
echo -e "$mess nThis is $dat"
      </pre>
      <p id="ch11Section_5_55">
       For me when I open the shell the output is something like:
      </p>
      <pre class="programlisting">
       Good Morning baali, Have a nice day!
This is Wednesday 16 in September of 2009 (11:54:47 AM IST)
      </pre>
     </div>
     <div class="section" title="6.4.Loops">
      <div class="titlepage">
       <div>
        <div>
         <h3 class="title">
          <a name="id2857492">
          </a>
          6.4.Loops
         </h3>
        </div>
       </div>
      </div>
      <p id="ch11Section_5_56">
       Bash has three different commands for looping --
       <code class="literal">
        for
       </code>
       ,
       <code class="literal">
        while
       </code>
       and
       <code class="literal">
        until
       </code>
       .
      </p>
      <div class="section" title="6.4.1.for loop">
       <div class="titlepage">
        <div>
         <div>
          <h4 class="title">
           <a name="id2857514">
           </a>
           6.4.1.
           <code class="literal">
            for
           </code>
           loop
          </h4>
         </div>
        </div>
       </div>
       <p id="ch11Section_5_57">
        Suppose we have a set of files, that have names beginning with numbers followed by their names -
        <code class="literal">
         08 - Society.mp3
        </code>
        . We would like to rename these files to remove the numbering. How would we go about doing that? It is clear from the problem statement that we could use a
        <code class="literal">
         for
        </code>
        loop, to loop through the list of files and rename each of the files.
       </p>
       <p id="ch11Section_5_58">
        Let's first look at a simple
        <code class="literal">
         for
        </code>
        loop, to understand how it works.
       </p>
       <pre class="programlisting">
        for animal in rat cat dog man
do
  echo $animal
done
       </pre>
       <p id="ch11Section_5_59">
        We just wrote a list of animals, each animal's name separated by a space and printed each name on a separate line. The variable
        <code class="literal">
         animal
        </code>
        is a dummy variable and has no significance. You could use something as lame as
        <code class="literal">
         i
        </code>
        in place of
        <code class="literal">
         animal
        </code>
        .
       </p>
       <p id="ch11Section_5_5a">
        Now, we use a simple
        <code class="literal">
         for
        </code>
        loop to list the files that we are interested in.
       </p>
       <pre class="programlisting">
        ls *.mp3 &gt; list
for i in `cat list`
do
  echo "$i"
done
       </pre>
       <p id="ch11Section_5_5b">
        If your filenames contain spaces,
        <code class="literal">
         for
        </code>
        assumes each space separated word to be a single item in the list and prints it in a separate line. We could change the script slightly to overcome this problem.
       </p>
       <pre class="programlisting">
        for i in *.mp3
do
  echo "$i"
done
       </pre>
       <p id="ch11Section_5_5c">
        Now, we have each file printed on a separate line. Depending on the files that we have we could use grep to get the relevant portion of the filenames and rename the files.
       </p>
       <pre class="programlisting">
        for i in *.mp3
do
  j=$(echo "$i"|grep -o "[A-Za-z'&amp;. ]*.mp3")
  echo "$i -&gt; $j"
done
       </pre>
       <p id="ch11Section_5_5d">
        Now we just replace the echo command with a
        <code class="literal">
         mv
        </code>
        or a
        <code class="literal">
         cp
        </code>
        command.
       </p>
       <pre class="programlisting">
        for i in *.mp3
do
  j=$(echo "$i"|grep -o "[A-Za-z'&amp;. ]*.mp3")
  cp "$i" "$j"
done
       </pre>
       <p id="ch11Section_5_5e">
        As an exercise, you could try sorting the files in reverse alphabetical order and then prefix numbers to each of the filenames.
       </p>
      </div>
      <div class="section" title="6.4.2.while">
       <div class="titlepage">
        <div>
         <div>
          <h4 class="title">
           <a name="id2857633">
           </a>
           6.4.2.
           <code class="literal">
            while
           </code>
          </h4>
         </div>
        </div>
       </div>
       <p id="ch11Section_5_5f">
        The
        <code class="literal">
         while
        </code>
        command allows us to continuously execute a block of commands until the command that is controlling the loop is executing successfully.
       </p>
       <p id="ch11Section_5_60">
        Let's start with the lamest example of a while loop.
       </p>
       <pre class="programlisting">
        while true
do
  echo "True"
done
       </pre>
       <p id="ch11Section_5_61">
        This, as you can see, is an infinite loop that prints the
        <code class="literal">
         True
        </code>
        .
       </p>
       <p id="ch11Section_5_62">
        Say we wish to write a simple program that takes user input and prints it back, until the input is
        <code class="literal">
         quit
        </code>
        , which quits the program.
       </p>
       <pre class="programlisting">
        while [ "$variable" != "quit" ]
do
  read variable
  echo "Input - $variable"
done
exit 0
       </pre>
      </div>
      <div class="section" title="6.4.3.until">
       <div class="titlepage">
        <div>
         <div>
          <h4 class="title">
           <a name="id2857683">
           </a>
           6.4.3.
           <code class="literal">
            until
           </code>
          </h4>
         </div>
        </div>
       </div>
       <p id="ch11Section_5_63">
        The
        <code class="literal">
         until
        </code>
        loop is similar to the
        <code class="literal">
         while
        </code>
        loop, except that it executes until the conditional command does not execute properly.
       </p>
       <p id="ch11Section_5_64">
        The infinite loop changes to the following, when
        <code class="literal">
         until
        </code>
        is used.
       </p>
       <pre class="programlisting">
        until false
do
  echo "True"
done
       </pre>
       <p id="ch11Section_5_65">
        Now lets try and use these above mentioned options provided by shell to write a utility. Until now, when we try find or locate it looks through directories and files for result. But they wont search through tar archives and zipped files. Lets create a shell script for especially looking through these files
       </p>
       <pre class="programlisting">
        #!/bin/sh

#To check number of arguments being passed.
if [ $# -eq 0 ] ; then
echo "Correct usage: $0 tar-archive filename  nOr $0 filename"
exit 1
else
  if [ $# -eq 1 ] ; then
    tar_archive=`find $PWD -name "*.tar*"`
  else
    tar_archive=`find $PWD -name $1`
  fi
fi

#Search of particular file inside archives.
for archive in $tar_archive
do
  echo $archive
  variable=`tar -tf $archive`
  for word in $variable
  do
    if [ $# -eq 1 ] ; then
      echo "$word" | grep -q ".*$1"
    else
      echo "$word" | grep -q ".*$2"
    fi
  if [ $? -eq 0 ] ; then
    echo "File present in $archive!"
  fi
  done
done
       </pre>
      </div>
     </div>
     <div class="section" title="6.5.Functions">
      <div class="titlepage">
       <div>
        <div>
         <h3 class="title">
          <a name="id2857738">
          </a>
          6.5.Functions
         </h3>
        </div>
       </div>
      </div>
      <p id="ch11Section_5_66">
       When a group of commands are repeatedly being used within a script, it is convenient to group them as a function. This saves a lot of time and you can avoid retyping the code again and again. Also, it will help you maintain your code easily. Let's see how we can define a simple function,
       <code class="literal">
        hello-world
       </code>
       . Functions can be defined in bash, either using the
       <code class="literal">
        function
       </code>
       built-in followed by the function name or just the function name followed by a pair of parentheses.
      </p>
      <pre class="programlisting">
       function hello-world
{
echo "Hello, World.";
}

hello-world () {
  echo "Hello, World.";
}

$ hello-world
Hello, World.
      </pre>
      <p id="ch11Section_5_67">
       Passing parameters to functions is similar to passing them to scripts.
      </p>
      <pre class="programlisting">
       function hello-name
{
echo "Hello, $1.";
}

$ hello-name 9
Hello, 9.
      </pre>
      <p id="ch11Section_5_68">
       Any variables that you define within a function, will be added to the global namespace. If you wish to define variables that are restricted to the scope of the function, define a variable using the
       <code class="literal">
        local
       </code>
       built-in command of bash.
      </p>
      <p id="ch11Section_5_69">
       We shall now write a function for the word frequency generating script that we had looked at in the previous session.
      </p>
      <pre class="programlisting">
       function word_frequency {
  if [ $# -ne 1 ]
  then
    echo "Usage: $0 file_name"
    exit 1
  else
    if [ -f "$1" ]
    then
      grep  "[A-Za-z]*" -o "$1" | tr 'A-Z' 'a-z' | sort | uniq -c | sort -nr | less
    fi
  fi
}
      </pre>
      <p id="ch11Section_5_6a">
       As an exercise, modify the function to accept the input for the number of top frequency words to be shown (if none is given, assume 10).
      </p>
     </div>
     <div class="section" title="6.6.Further Reading:">
      <div class="titlepage">
       <div>
        <div>
         <h3 class="title">
          <a name="id2857809">
          </a>
          6.6.Further Reading:
         </h3>
        </div>
       </div>
      </div>
      <div class="itemizedlist">
       <ul class="itemizedlist" type="*">
        <li class="listitem" style="list-style-type: *">
         <p id="ch11Section_5_6b">
         </p>
         <div class="reference">
          <div class="titlepage">
           <hr />
          </div>
          http://www.freeos.com/guides/lsst/
         </div>
        </li>
        <li class="listitem" style="list-style-type: *">
         <p id="ch11Section_5_6c">
         </p>
         <div class="reference">
          <div class="titlepage">
           <hr />
          </div>
          http://bash.cyberciti.biz/guide/Main_Page
         </div>
        </li>
        <li class="listitem" style="list-style-type: *">
         <p id="ch11Section_5_6d">
         </p>
         <div class="reference">
          <div class="titlepage">
           <hr />
          </div>
          http://tldp.org/LDP/abs/html/
         </div>
        </li>
        <li class="listitem" style="list-style-type: *">
         <p id="ch11Section_5_6e">
         </p>
         <div class="reference">
          <div class="titlepage">
           <hr />
          </div>
          http://tldp.org/LDP/Bash-Beginners-Guide/html/Bash-Beginners-Guide.html
         </div>
        </li>
       </ul>
      </div>
     </div>
    </div>
   </div>
  </div>
 </body>
</html>