SEESenv/web/html/chn11Section_5.html
author amit@thunder
Thu, 25 Feb 2010 00:45:20 +0530
changeset 28 514098969b11
parent 27 cb14131583c6
permissions -rw-r--r--
Testing
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
672eaaab9204 Added some new html files that failed earlier during change from rst
amit@thunder
parents:
diff changeset
     1
<html>
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
     2
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
     3
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="chapter" id="ch11Section_5">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
     4
<div class="titlepage"></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
     5
<div class="toc">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
     6
<p><b>Table of Contents</b></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
     7
<dl>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
     8
<dt><span class="article"><a href="#id2647624"></a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
     9
<dd><dl>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    10
<dt><span class="section"><a href="#id2457715">1. Module Objectives:</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    11
<dt><span class="section"><a href="#id2645976">2. tar:</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    12
<dd><dl>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    13
<dt><span class="section"><a href="#id2646529">2.1. Introduction:</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    14
<dt><span class="section"><a href="#id2698596">2.2. Getting Started(go go go!):</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    15
<dt><span class="section"><a href="#id2698875">2.3. Further Reading for this section:</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    16
</dl></dd>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    17
<dt><span class="section"><a href="#id2698920">3. GZip:</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    18
<dd><dl>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    19
<dt><span class="section"><a href="#id2698946">3.1. Get going:</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    20
<dt><span class="section"><a href="#id2699199">3.2. Further Reading for this section:</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    21
</dl></dd>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    22
<dt><span class="section"><a href="#id2699253">4. File Comparisons:</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    23
<dd><dl>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    24
<dt><span class="section"><a href="#id2699270">4.1. cmp:</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    25
<dt><span class="section"><a href="#id2699312">4.2. diff:</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    26
<dt><span class="section"><a href="#id2699394">4.3. comm:</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    27
</dl></dd>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    28
<dt><span class="section"><a href="#id2699424">5. Environment Variables:</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    29
<dd><dl><dt><span class="section"><a href="#id2699524">5.1. Further Reading:</a></span></dt></dl></dd>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    30
<dt><span class="section"><a href="#id2699578">6. Shell Scripting:</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    31
<dd><dl>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    32
<dt><span class="section"><a href="#id2699586">6.1. Basics:</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    33
<dt><span class="section"><a href="#id2697944">6.2. Shell Arithmetic:</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    34
<dt><span class="section"><a href="#id2699963">6.3. if else construct:</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    35
<dt><span class="section"><a href="#id2700069">6.4. Loops</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    36
<dt><span class="section"><a href="#id2700315">6.5. Functions</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    37
<dt><span class="section"><a href="#id2700386">6.6. Further Reading:</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    38
</dl></dd>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    39
</dl></dd>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    40
</dl>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    41
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    42
<div class="article">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    43
<div class="titlepage">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    44
<div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    45
<div><h2 class="title" id="id2647624"></h2></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    46
<div><h2 class="title" id="id2647624"></h2></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    47
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    48
<hr />
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    49
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    50
<div class="toc">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    51
<p><b>Table of Contents</b></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    52
<dl>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    53
<dt><span class="section"><a href="#id2457715">1. Module Objectives:</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    54
<dt><span class="section"><a href="#id2645976">2. tar:</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    55
<dd><dl>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    56
<dt><span class="section"><a href="#id2646529">2.1. Introduction:</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    57
<dt><span class="section"><a href="#id2698596">2.2. Getting Started(go go go!):</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    58
<dt><span class="section"><a href="#id2698875">2.3. Further Reading for this section:</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    59
</dl></dd>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    60
<dt><span class="section"><a href="#id2698920">3. GZip:</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    61
<dd><dl>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    62
<dt><span class="section"><a href="#id2698946">3.1. Get going:</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    63
<dt><span class="section"><a href="#id2699199">3.2. Further Reading for this section:</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    64
</dl></dd>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    65
<dt><span class="section"><a href="#id2699253">4. File Comparisons:</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    66
<dd><dl>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    67
<dt><span class="section"><a href="#id2699270">4.1. cmp:</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    68
<dt><span class="section"><a href="#id2699312">4.2. diff:</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    69
<dt><span class="section"><a href="#id2699394">4.3. comm:</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    70
</dl></dd>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    71
<dt><span class="section"><a href="#id2699424">5. Environment Variables:</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    72
<dd><dl><dt><span class="section"><a href="#id2699524">5.1. Further Reading:</a></span></dt></dl></dd>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    73
<dt><span class="section"><a href="#id2699578">6. Shell Scripting:</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    74
<dd><dl>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    75
<dt><span class="section"><a href="#id2699586">6.1. Basics:</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    76
<dt><span class="section"><a href="#id2697944">6.2. Shell Arithmetic:</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    77
<dt><span class="section"><a href="#id2699963">6.3. if else construct:</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    78
<dt><span class="section"><a href="#id2700069">6.4. Loops</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    79
<dt><span class="section"><a href="#id2700315">6.5. Functions</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    80
<dt><span class="section"><a href="#id2700386">6.6. Further Reading:</a></span></dt>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    81
</dl></dd>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    82
</dl>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    83
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    84
<div class="section" title="1.Module Objectives:">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    85
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    86
<a name="id2457715"></a>1.Module Objectives:</h2></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    87
<p id="ch11Section_5_1">After successfully completing this module a participant will be able to:</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    88
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    89
- Understand
25
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
    90
  * What are archives and zipped files                              U
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
    91
  * What are environment variables                                  U
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
    92
  * What are Shell Scripts                                          U
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
    93
- Able to use file comparison commands like                         Ap
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
    94
  diff, cmp, comm
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
    95
- Create and extract archives(.tar files) and zipped files(.gz)     Ap
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
    96
- Set/Modify environment as per need                                Ap
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    97
- Create shell scripts to automate tasks.                           Ap</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    98
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
    99
<div class="section" title="2.tar:">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   100
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   101
<a name="id2645976"></a>2.tar:</h2></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   102
<div class="section" title="2.1.Introduction:">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   103
<div class="titlepage"><div><div><h3 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   104
<a name="id2646529"></a>2.1.Introduction:</h3></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   105
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   106
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   107
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   108
<div class="section" title="2.2.Getting Started(go go go!):">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   109
<div class="titlepage"><div><div><h3 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   110
<a name="id2698596"></a>2.2.Getting Started(go go go!):</h3></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   111
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   112
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   113
$ man tar</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   114
<p id="ch11Section_5_5">or try these commands(the output may vary with different installations):</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   115
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   116
$ tar --version
25
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   117
tar (GNU tar) 1.20
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   118
Copyright (C) 2008 Free Software Foundation, Inc.
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   119
License GPLv3+: GNU GPL version 3 or later &lt;http://gnu.org/licenses/gpl.html&gt;
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   120
This is free software: you are free to change and redistribute it.
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   121
There is NO WARRANTY, to the extent permitted by law.
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   122
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   123
Written by John Gilmore and Jay Fenlason.
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   124
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   125
$ tar --help
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   126
Usage: tar [OPTION...] [FILE]...
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   127
GNU `tar' saves many files together into a single tape or disk archive, and can
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   128
restore individual files from the archive.
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   129
Examples:
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   130
tar -cf archive.tar foo bar  # Create archive.tar from files foo and bar.
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   131
tar -tvf archive.tar         # List all files in archive.tar verbosely.
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   132
tar -xf archive.tar          # Extract all files from archive.tar.
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   133
____________</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   134
<div class="section" title="2.2.1.Creating a tar archive:">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   135
<div class="titlepage"><div><div><h4 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   136
<a name="id2698650"></a>2.2.1.Creating a tar archive:</h4></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   137
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   138
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   139
$ mkdir fortune-files
25
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   140
$ cd fortune-files/
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   141
$ fortune &gt; first.txt
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   142
$ cat first.txt
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   143
Expect the worst, it's the least you can do.
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   144
$ fortune &gt; second.txt
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   145
$ fortune &gt; third.txt
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   146
$ ls
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   147
first.txt  second.txt  third.txt</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   148
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   149
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   150
$ tar --create --verbose --file=allfiles.tar first.txt second.txt third.txt
25
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   151
first.txt
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   152
second.txt
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   153
third.txt
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   154
$ ls
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   155
allfiles.tar  first.txt  second.txt  third.txt</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   156
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   157
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   158
$ tar -c -v -f allfiles.tar first.txt second.txt third.txt</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   159
<p id="ch11Section_5_9">or</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   160
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   161
$ tar -cvf allfiles.tar first.txt second.txt third.txt</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   162
<p id="ch11Section_5_a">The general format for creating a tar archive is:</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   163
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   164
tar [OPTION...] [FILE]...</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   165
<p id="ch11Section_5_b">For our command are using these options:</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   166
<div class="itemizedlist"><ul class="itemizedlist" type="*">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   167
<li class="listitem" style="list-style-type: *"><p id="ch11Section_5_c">-c to Create the archive.</p></li>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   168
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   169
<li class="listitem" style="list-style-type: *"><p id="ch11Section_5_e">-f mentioning the file name of the resulting tar archive.</p></li>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   170
</ul></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   171
<p id="ch11Section_5_f">To create archive of folder itself try this:</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   172
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   173
$ tar -cvf fortune.tar fortune/</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   174
<p id="ch11Section_5_10">To add files to existing tar archive, option <span class="emphasis"><em>`r`</em></span> is used:</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   175
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   176
$ fortune &gt; fourth.txt
25
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   177
$ tar -r fourth.txt -vf allfiles.tar
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   178
fourth.txt</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   179
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   180
<p id="ch11Section_5_12">Similarly to remove file from archive use <span class="emphasis"><em>--delete</em></span> option:</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   181
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   182
$ tar --delete second.txt -f allfiles.tar
25
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   183
$ tar -tf allfiles.tar
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   184
first.txt
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   185
third.txt
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   186
fourth.txt</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   187
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   188
<div class="section" title="2.2.2.Listing the files of archive:">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   189
<div class="titlepage"><div><div><h4 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   190
<a name="id2698790"></a>2.2.2.Listing the files of archive:</h4></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   191
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   192
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   193
$ tar tf allfiles.tar
25
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   194
first.txt
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   195
second.txt
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   196
third.txt</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   197
<p id="ch11Section_5_14"><span class="strong"><strong>//this is not working for me in some cases :(</strong></span></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   198
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   199
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   200
$ tar t second.txt allfiles.tar
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   201
second.txt</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   202
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   203
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   204
<div class="section" title="2.2.3.Extracting files from archive:">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   205
<div class="titlepage"><div><div><h4 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   206
<a name="id2698840"></a>2.2.3.Extracting files from archive:</h4></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   207
<p id="ch11Section_5_17">To extract the content from a archive, use <span class="emphasis"><em>`x`</em></span> option:</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   208
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   209
$ mkdir extract
25
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   210
$ cp allfiles.tar extract/
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   211
$ cd extract
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   212
$ tar -xvf allfiles.tar
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   213
first.txt
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   214
second.txt
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   215
third.txt</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   216
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   217
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   218
$ tar -x second.txt -vf allfiles.tar
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   219
second.txt</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   220
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   221
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   222
<div class="section" title="2.3.Further Reading for this section:">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   223
<div class="titlepage"><div><div><h3 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   224
<a name="id2698875"></a>2.3.Further Reading for this section:</h3></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   225
<div class="itemizedlist"><ul class="itemizedlist" type="*">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   226
<li class="listitem" style="list-style-type: *">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   227
<p id="ch11Section_5_19"></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   228
<div class="reference">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   229
<div class="titlepage"><hr /></div>http://en.wikipedia.org/wiki/Tar_(file_format</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   230
<p>)</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   231
</li>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   232
<li class="listitem" style="list-style-type: *">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   233
<p id="ch11Section_5_1a"></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   234
<div class="reference">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   235
<div class="titlepage"><hr /></div>http://www.gnu.org/software/tar/manual/tar.html</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   236
</li>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   237
<li class="listitem" style="list-style-type: *">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   238
<p id="ch11Section_5_1b"></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   239
<div class="reference">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   240
<div class="titlepage"><hr /></div>http://linuxreviews.org/beginner/</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   241
</li>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   242
</ul></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   243
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   244
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   245
<div class="section" title="3.GZip:">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   246
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   247
<a name="id2698920"></a>3.GZip:</h2></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   248
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   249
<div class="section" title="3.1.Get going:">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   250
<div class="titlepage"><div><div><h3 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   251
<a name="id2698946"></a>3.1.Get going:</h3></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   252
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   253
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   254
$ man gzip
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   255
$ gzip --help</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   256
<p id="ch11Section_5_1e">Creating a zip of a bunch of files is fairly trivial, it can be done simply via:</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   257
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   258
$ gzip [OPTION]... [FILE]...</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   259
<div class="section" title="3.1.1.Creating zip files:">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   260
<div class="titlepage"><div><div><h4 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   261
<a name="id2698976"></a>3.1.1.Creating zip files:</h4></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   262
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   263
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   264
$ gzip first.txt fourth.txt second.txt third.txt
25
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   265
$ ls
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   266
allfiles.tar  first.txt.gz  fourth.txt.gz  second.txt.gz  third.txt.gz  zipped.tar.gz</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   267
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   268
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   269
$ gzip *.txt</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   270
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   271
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   272
$ gzip -v *.txt
25
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   273
first.txt:    4.4% -- replaced with first.txt.gz
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   274
fourth.txt:  -7.1% -- replaced with fourth.txt.gz
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   275
second.txt:  -4.8% -- replaced with second.txt.gz
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   276
third.txt:    3.8% -- replaced with third.txt.gz</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   277
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   278
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   279
$ gzip -c *.txt &gt; all.gz</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   280
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   281
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   282
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   283
$ gzip -r fortune-files/
25
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   284
$ gzip -rv .
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   285
./first.txt:          4.4% -- replaced with ./first.txt.gz
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   286
./second.txt:        -4.8% -- replaced with ./second.txt.gz
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   287
./third.txt:          3.8% -- replaced with ./third.txt.gz
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   288
./allfiles.tar:      96.6% -- replaced with ./allfiles.tar.gz
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   289
./fourth.txt:        -7.1% -- replaced with ./fourth.txt.gz</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   290
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   291
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   292
$ tar -cvzf zipped.tar.gz *.txt
25
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   293
first.txt
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   294
fourth.txt
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   295
second.txt
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   296
third.txt</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   297
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   298
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   299
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   300
$ gzip -l zipped.tar.gz
25
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   301
         compressed        uncompressed  ratio uncompressed_name
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   302
            332               10240      97.0% zipped.tar</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   303
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   304
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   305
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   306
$ gzip -dv *.gz
25
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   307
all.gz:     -440.4% -- replaced with all
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   308
first.txt.gz:         4.4% -- replaced with first.txt
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   309
fourth.txt.gz:       -7.1% -- replaced with fourth.txt
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   310
second.txt.gz:       -4.8% -- replaced with second.txt
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   311
third.txt.gz:         3.8% -- replaced with third.txt
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   312
zipped.tar.gz:       97.0% -- replaced with zipped.tar</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   313
<p id="ch11Section_5_2a">or:</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   314
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   315
$ gunzip -v *.gz</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   316
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   317
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   318
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   319
<div class="section" title="3.2.Further Reading for this section:">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   320
<div class="titlepage"><div><div><h3 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   321
<a name="id2699199"></a>3.2.Further Reading for this section:</h3></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   322
<div class="itemizedlist"><ul class="itemizedlist" type="*">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   323
<li class="listitem" style="list-style-type: *">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   324
<p id="ch11Section_5_2c"></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   325
<div class="reference">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   326
<div class="titlepage"><hr /></div>http://linuxreviews.org/beginner/</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   327
</li>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   328
<li class="listitem" style="list-style-type: *">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   329
<p id="ch11Section_5_2d"></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   330
<div class="reference">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   331
<div class="titlepage"><hr /></div>http://lowfatlinux.com/linux-gzip-gunzip.html</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   332
</li>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   333
<li class="listitem" style="list-style-type: *">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   334
<p id="ch11Section_5_2e"></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   335
<div class="reference">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   336
<div class="titlepage"><hr /></div>http://www.gnu.org/software/gzip/manual/gzip.html</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   337
</li>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   338
<li class="listitem" style="list-style-type: *">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   339
<p id="ch11Section_5_2f"></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   340
<div class="reference">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   341
<div class="titlepage"><hr /></div>http://en.wikipedia.org/wiki/ZIP_(file_format</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   342
<p>)</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   343
</li>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   344
</ul></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   345
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   346
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   347
<div class="section" title="4.File Comparisons:">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   348
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   349
<a name="id2699253"></a>4.File Comparisons:</h2></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   350
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   351
<div class="section" title="4.1.cmp:">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   352
<div class="titlepage"><div><div><h3 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   353
<a name="id2699270"></a>4.1.cmp:</h3></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   354
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   355
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   356
$ find . -name quick.c
25
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   357
./Desktop/programs/quick.c
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   358
./c-folder/quick.c
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   359
$ cmp Desktop/programs/quick.c c-folder/quick.c
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   360
$</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   361
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   362
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   363
$ cmp Desktop/programs/quick.c c-folder/quick.c
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   364
Desktop/programs/quick.c c-folder/quick.c differ: byte 339, line 24</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   365
<p id="ch11Section_5_33">That is, if files differ, the byte and line number at which the first difference occurred is reported.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   366
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   367
<div class="section" title="4.2.diff:">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   368
<div class="titlepage"><div><div><h3 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   369
<a name="id2699312"></a>4.2.diff:</h3></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   370
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   371
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   372
$ echo -e "quick nbrown nfox njumped nover nthe nlazy ndog" &gt; allcharacters.txt
25
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   373
$ echo -e "quick nbrown nfox njmuped nover nteh nlzay ndog" &gt; problem.txt
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   374
$ diff problem.txt allcharacters.txt
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   375
4c4
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   376
&lt; jmuped
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   377
---
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   378
&gt; jumped
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   379
6,7c6,7
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   380
&lt; teh
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   381
&lt; lzay
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   382
---
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   383
&gt; the
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   384
&gt; lazy</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   385
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   386
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   387
$ sdiff problem.txt allcharacters.txt
25
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   388
quick                                                               quick
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   389
brown                                                               brown
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   390
fox                                                                 fox
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   391
jmuped                                                            | jumped
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   392
over                                                                over
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   393
teh                                                               | the
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   394
lzay                                                              | lazy
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   395
dog                                                                 dog</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   396
<p id="ch11Section_5_36">Some exercise for a change:</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   397
<div class="itemizedlist"><ul class="itemizedlist" type="*">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   398
<li class="listitem" style="list-style-type: *"><p id="ch11Section_5_37">Try using diff for any binary file, does it work?</p></li>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   399
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   400
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   401
</ul></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   402
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   403
<div class="section" title="4.3.comm:">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   404
<div class="titlepage"><div><div><h3 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   405
<a name="id2699394"></a>4.3.comm:</h3></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   406
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   407
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   408
$ sort allcharacters.txt&gt;sortedcharac.txt; sort problem.txt&gt;sortedprob.txt
25
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   409
$ comm sortedcharac.txt sortedprob.txt
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   410
             brown
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   411
             dog
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   412
             fox
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   413
     jmuped
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   414
jumped
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   415
lazy
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   416
     lzay
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   417
             over
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   418
             quick
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   419
     teh
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   420
the</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   421
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   422
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   423
<div class="section" title="5.Environment Variables:">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   424
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   425
<a name="id2699424"></a>5.Environment Variables:</h2></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   426
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   427
<p id="ch11Section_5_3c">Some of examples of Environment variables are(result may vary!):</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   428
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   429
$ echo $OSTYPE
25
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   430
linux-gnu
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   431
$ echo $HOME
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   432
/home/baali</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   433
<p id="ch11Section_5_3d">To see all the variables and there values use any of following commands:</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   434
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   435
$ printenv | less
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   436
$ env</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   437
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   438
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   439
$ set PATH=$PATH:$HOME/bin</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   440
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   441
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   442
$ set repo = $HOME/Desktop/random/code
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   443
$ cd $repo</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   444
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   445
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   446
<div class="section" title="5.1.Further Reading:">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   447
<div class="titlepage"><div><div><h3 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   448
<a name="id2699524"></a>5.1.Further Reading:</h3></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   449
<div class="itemizedlist"><ul class="itemizedlist" type="*">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   450
<li class="listitem" style="list-style-type: *">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   451
<p id="ch11Section_5_42"></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   452
<div class="reference">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   453
<div class="titlepage"><hr /></div>http://lowfatlinux.com/linux-environment-variables.html</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   454
</li>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   455
<li class="listitem" style="list-style-type: *">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   456
<p id="ch11Section_5_43"></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   457
<div class="reference">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   458
<div class="titlepage"><hr /></div>http://www.codecoffee.com/tipsforlinux/articles/030.html</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   459
</li>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   460
<li class="listitem" style="list-style-type: *">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   461
<p id="ch11Section_5_44"></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   462
<div class="reference">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   463
<div class="titlepage"><hr /></div>http://www.ee.surrey.ac.uk/Teaching/Unix/unix8.html</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   464
</li>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   465
<li class="listitem" style="list-style-type: *">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   466
<p id="ch11Section_5_45"></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   467
<div class="reference">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   468
<div class="titlepage"><hr /></div>http://en.wikipedia.org/wiki/Environment_variable</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   469
</li>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   470
</ul></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   471
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   472
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   473
<div class="section" title="6.Shell Scripting:">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   474
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   475
<a name="id2699578"></a>6.Shell Scripting:</h2></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   476
<div class="section" title="6.1.Basics:">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   477
<div class="titlepage"><div><div><h3 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   478
<a name="id2699586"></a>6.1.Basics:</h3></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   479
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   480
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   481
$ echo '#!/bin/sh' &gt; my-script.sh
25
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   482
$ clear &gt;&gt; my-script.sh
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   483
$ echo 'echo Hello World' &gt;&gt; my-script.sh
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   484
$ chmod 755 my-script.sh
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   485
$ ./my-script.sh
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   486
Hello World</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   487
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   488
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   489
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   490
#!/bin/sh
25
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   491
ls &gt; temp
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   492
grep ^$1 &lt; temp
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   493
rm temp
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   494
$ chmod a+x initial.sh
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   495
$ ./initial.sh s</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   496
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   497
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   498
#!/bin/sh
25
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   499
name=`find . -name $1 -print`
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   500
echo $name
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   501
last_modified=`stat -c %y $name| cut -f 1 -d " "`
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   502
echo "Last modified: $last_modified"
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   503
$ ./search.sh fname</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   504
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   505
<p id="ch11Section_5_4b">naming conventions for variables?? do we need them??</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   506
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   507
<div class="section" title="6.2.Shell Arithmetic:">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   508
<div class="titlepage"><div><div><h3 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   509
<a name="id2697944"></a>6.2.Shell Arithmetic:</h3></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   510
<p id="ch11Section_5_4c">Shell also provides support for basic arithmetic operations. The syntax is:</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   511
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   512
$ expr op1 math-operator op2</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   513
<p id="ch11Section_5_4d">Some of example which can be tried handily:</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   514
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   515
$ expr -3 + 5
25
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   516
2
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   517
$ expr 10 % 3
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   518
1</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   519
<p id="ch11Section_5_4e">These spaces in between operator and operands is important, without them shell interpreter will raise the syntax error.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   520
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   521
$ expr 2*3
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   522
expr: syntax error</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   523
<p id="ch11Section_5_4f">One can use back-quotes(`) also to get value of expr.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   524
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   525
$ echo `expr 6 + 3`
25
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   526
9
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   527
$ result=`expr 6 + 3`
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   528
$ echo $result
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   529
9</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   530
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   531
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   532
$ echo "Today is date"
25
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   533
Today is date
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   534
$ echo "Today is `date`"
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   535
Today is Wed Sep 16 17:32:22 IST 2009
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   536
$ echo 'Today is `date`'
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   537
Today is `date`
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   538
$ echo "Today is  n `date`"
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   539
Today is  n Wed Sep 16 17:40:13 IST 2009
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   540
$ echo -e "Today is  n `date`"
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   541
Today is
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   542
 Wed Sep 16 17:41:13 IST 2009</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   543
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   544
<div class="section" title="6.3.if else construct:">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   545
<div class="titlepage"><div><div><h3 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   546
<a name="id2699963"></a>6.3.if else construct:</h3></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   547
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   548
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   549
#!/bin/sh
25
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   550
if test $1 -gt 0
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   551
then
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   552
  echo "number is positive"
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   553
else
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   554
  echo "number is negative"
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   555
fi
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   556
$ ./sign.sh -11
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   557
number is negative</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   558
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   559
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   560
#this is the case when no argument is passed
25
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   561
if [ $# -eq 0 ]
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   562
then
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   563
  echo "$0 : You must give/supply one integers"
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   564
  exit 1
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   565
else
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   566
  if [ $# -gt 1 ]
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   567
  then
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   568
    echo "$0 : You must give one integer"
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   569
    exit 1
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   570
  fi
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   571
fi</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   572
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   573
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   574
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   575
#!/bin/sh
25
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   576
#Script to greet the user according to time of day
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   577
temph=`date | cut -c12-13`
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   578
dat=`date +"%A %d in %B of %Y (%r)"`
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   579
if [ $temph -lt 12 ]
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   580
then
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   581
  mess="Good Morning $LOGNAME, Have a nice day!"
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   582
fi
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   583
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   584
if [ $temph -gt 12 -a $temph -le 16 ]
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   585
then
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   586
  mess="Good Afternoon $LOGNAME"
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   587
fi
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   588
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   589
if [ $temph -gt 16 -a $temph -le 18 ]
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   590
then
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   591
  mess="Good Evening $LOGNAME"
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   592
fi
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   593
echo -e "$mess nThis is $dat"</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   594
<p id="ch11Section_5_55">For me when I open the shell the output is something like:</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   595
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   596
Good Morning baali, Have a nice day!
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   597
This is Wednesday 16 in September of 2009 (11:54:47 AM IST)</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   598
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   599
<div class="section" title="6.4.Loops">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   600
<div class="titlepage"><div><div><h3 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   601
<a name="id2700069"></a>6.4.Loops</h3></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   602
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   603
<div class="section" title="6.4.1.for loop">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   604
<div class="titlepage"><div><div><h4 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   605
<a name="id2700092"></a>6.4.1.<code class="literal">for</code> loop</h4></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   606
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   607
<p id="ch11Section_5_58">Let's first look at a simple <code class="literal">for</code> loop, to understand how it works.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   608
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   609
for animal in rat cat dog man
25
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   610
do
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   611
  echo $animal
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   612
done</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   613
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   614
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   615
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   616
ls *.mp3 &gt; list
25
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   617
for i in `cat list`
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   618
do
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   619
  echo "$i"
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   620
done</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   621
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   622
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   623
for i in *.mp3
25
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   624
do
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   625
  echo "$i"
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   626
done</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   627
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   628
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   629
for i in *.mp3
25
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   630
do
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   631
  j=$(echo "$i"|grep -o "[A-Za-z'&amp;. ]*.mp3")
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   632
  echo "$i -&gt; $j"
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   633
done</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   634
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   635
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   636
for i in *.mp3
25
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   637
do
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   638
  j=$(echo "$i"|grep -o "[A-Za-z'&amp;. ]*.mp3")
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   639
  cp "$i" "$j"
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   640
done</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   641
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   642
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   643
<div class="section" title="6.4.2.while">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   644
<div class="titlepage"><div><div><h4 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   645
<a name="id2700210"></a>6.4.2.<code class="literal">while</code>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   646
</h4></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   647
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   648
<p id="ch11Section_5_60">Let's start with the lamest example of a while loop.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   649
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   650
while true
25
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   651
do
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   652
  echo "True"
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   653
done</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   654
<p id="ch11Section_5_61">This, as you can see, is an infinite loop that prints the <code class="literal">True</code>.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   655
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   656
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   657
while [ "$variable" != "quit" ]
25
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   658
do
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   659
  read variable
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   660
  echo "Input - $variable"
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   661
done
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   662
exit 0</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   663
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   664
<div class="section" title="6.4.3.until">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   665
<div class="titlepage"><div><div><h4 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   666
<a name="id2700260"></a>6.4.3.<code class="literal">until</code>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   667
</h4></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   668
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   669
<p id="ch11Section_5_64">The infinite loop changes to the following, when <code class="literal">until</code> is used.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   670
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   671
until false
25
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   672
do
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   673
  echo "True"
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   674
done</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   675
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   676
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   677
#!/bin/sh
25
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   678
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   679
#To check number of arguments being passed.
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   680
if [ $# -eq 0 ] ; then
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   681
echo "Correct usage: $0 tar-archive filename  nOr $0 filename"
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   682
exit 1
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   683
else
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   684
  if [ $# -eq 1 ] ; then
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   685
    tar_archive=`find $PWD -name "*.tar*"`
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   686
  else
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   687
    tar_archive=`find $PWD -name $1`
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   688
  fi
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   689
fi
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   690
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   691
#Search of particular file inside archives.
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   692
for archive in $tar_archive
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   693
do
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   694
  echo $archive
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   695
  variable=`tar -tf $archive`
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   696
  for word in $variable
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   697
  do
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   698
    if [ $# -eq 1 ] ; then
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   699
      echo "$word" | grep -q ".*$1"
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   700
    else
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   701
      echo "$word" | grep -q ".*$2"
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   702
    fi
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   703
  if [ $? -eq 0 ] ; then
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   704
    echo "File present in $archive!"
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   705
  fi
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   706
  done
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   707
done</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   708
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   709
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   710
<div class="section" title="6.5.Functions">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   711
<div class="titlepage"><div><div><h3 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   712
<a name="id2700315"></a>6.5.Functions</h3></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   713
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   714
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   715
function hello-world
25
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   716
{
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   717
echo "Hello, World.";
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   718
}
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   719
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   720
hello-world () {
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   721
  echo "Hello, World.";
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   722
}
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   723
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   724
$ hello-world
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   725
Hello, World.</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   726
<p id="ch11Section_5_67">Passing parameters to functions is similar to passing them to scripts.</p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   727
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   728
function hello-name
25
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   729
{
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   730
echo "Hello, $1.";
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   731
}
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   732
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   733
$ hello-name 9
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   734
Hello, 9.</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   735
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   736
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   737
<pre class="programlisting">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   738
function word_frequency {
25
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   739
  if [ $# -ne 1 ]
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   740
  then
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   741
    echo "Usage: $0 file_name"
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   742
    exit 1
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   743
  else
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   744
    if [ -f "$1" ]
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   745
    then
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   746
      grep  "[A-Za-z]*" -o "$1" | tr 'A-Z' 'a-z' | sort | uniq -c | sort -nr | less
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   747
    fi
ed38dd9bdb50 New build of html solving some of the bugs
amit@thunder
parents: 16
diff changeset
   748
  fi
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   749
}</pre>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   750
<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>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   751
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   752
<div class="section" title="6.6.Further Reading:">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   753
<div class="titlepage"><div><div><h3 class="title">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   754
<a name="id2700386"></a>6.6.Further Reading:</h3></div></div></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   755
<div class="itemizedlist"><ul class="itemizedlist" type="*">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   756
<li class="listitem" style="list-style-type: *">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   757
<p id="ch11Section_5_6b"></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   758
<div class="reference">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   759
<div class="titlepage"><hr /></div>http://www.freeos.com/guides/lsst/</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   760
</li>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   761
<li class="listitem" style="list-style-type: *">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   762
<p id="ch11Section_5_6c"></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   763
<div class="reference">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   764
<div class="titlepage"><hr /></div>http://bash.cyberciti.biz/guide/Main_Page</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   765
</li>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   766
<li class="listitem" style="list-style-type: *">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   767
<p id="ch11Section_5_6d"></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   768
<div class="reference">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   769
<div class="titlepage"><hr /></div>http://tldp.org/LDP/abs/html/</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   770
</li>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   771
<li class="listitem" style="list-style-type: *">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   772
<p id="ch11Section_5_6e"></p>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   773
<div class="reference">
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   774
<div class="titlepage"><hr /></div>http://tldp.org/LDP/Bash-Beginners-Guide/html/Bash-Beginners-Guide.html</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   775
</li>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   776
</ul></div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   777
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   778
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   779
</div>
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   780
</div></body>
1
672eaaab9204 Added some new html files that failed earlier during change from rst
amit@thunder
parents:
diff changeset
   781
</html>
28
514098969b11 Testing
amit@thunder
parents: 27
diff changeset
   782