ult/Section_5.rst
author Puneeth Chaganti <puneeth@fossee.in>
Fri, 18 Sep 2009 15:49:06 +0530
changeset 62 780effcd7c5a
parent 60 8a36825e21c5
child 63 dc13f22e9489
permissions -rw-r--r--
fixed table in session4.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
54
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     1
Module Objectives:
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     2
==================
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     3
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     4
After successfully completing this module a participant will be able to: ::
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     5
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     6
  - Understand
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     7
    * What are archives and zipped files                              U
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     8
    * What are environment variables				      U
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     9
    * What are Shell Scripts					      U
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    10
  - Able to use file comparison commands like 			      Ap
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    11
    diff, cmp, comm
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    12
  - Create and extract archives(.tar files) and zipped files(.gz)     Ap
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    13
  - Set/Modify environment as per need	    	                      Ap
60
8a36825e21c5 Arithmetic section, typo correction, eg script.
Shantanu <shantanu@fossee.in>
parents: 59
diff changeset
    14
  - Create shell scripts to automate tasks.			      Ap
54
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    15
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    16
tar:
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    17
====
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    18
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    19
Introduction:
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    20
-------------
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    21
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    22
In world of Linux based distribution, *tarballs* 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 *tar* archives is to: *Store, backup, and transport*.
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    23
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    24
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. 
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    25
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    26
Getting Started(go go go!):
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    27
---------------------------
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    28
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    29
As mentioned previously and if not, *The best way to get started with any command line tool of Linux is to use "man".* ::
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    30
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    31
   $ man tar
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    32
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    33
or try these commands(the output may vary with different installations): ::
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    34
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    35
   $ tar --version
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    36
   tar (GNU tar) 1.20
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    37
   Copyright (C) 2008 Free Software Foundation, Inc.
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    38
   License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    39
   This is free software: you are free to change and redistribute it.
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    40
   There is NO WARRANTY, to the extent permitted by law.
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    41
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    42
   Written by John Gilmore and Jay Fenlason.
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    43
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    44
   $ tar --help
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    45
   Usage: tar [OPTION...] [FILE]...
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    46
   GNU `tar' saves many files together into a single tape or disk archive, and can
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    47
   restore individual files from the archive.
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    48
   Examples:
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    49
   tar -cf archive.tar foo bar  # Create archive.tar from files foo and bar.
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    50
   tar -tvf archive.tar         # List all files in archive.tar verbosely.
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    51
   tar -xf archive.tar          # Extract all files from archive.tar.  
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    52
   ____________
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    53
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    54
Creating a tar archive: 
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    55
~~~~~~~~~~~~~~~~~~~~~~~
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    56
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    57
We will do some off-the road activity for this exercise. We will use an interesting command *fortune* 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. ::
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    58
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    59
   $ mkdir fortune-files 
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    60
   $ cd fortune-files/
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    61
   $ fortune > first.txt
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    62
   $ cat first.txt 
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    63
   Expect the worst, it's the least you can do.
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    64
   $ fortune > second.txt
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    65
   $ fortune > third.txt
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    66
   $ ls
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    67
   first.txt  second.txt  third.txt
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    68
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    69
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: ::
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    70
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    71
   $ tar --create --verbose --file=allfiles.tar first.txt second.txt third.txt
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    72
   first.txt
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    73
   second.txt
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    74
   third.txt
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    75
   $ ls
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    76
   allfiles.tar  first.txt  second.txt  third.txt
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    77
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    78
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: ::
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    79
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    80
   $ tar -c -v -f allfiles.tar first.txt second.txt third.txt
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    81
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    82
or ::
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    83
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    84
   $ tar -cvf allfiles.tar first.txt second.txt third.txt
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    85
   
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    86
The general format for creating a tar archive is: ::
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    87
   
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    88
   tar [OPTION...] [FILE]... 
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    89
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    90
For our command are using these options:
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    91
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    92
   * -c to Create the archive.
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    93
   * -v for Verbose mode, to get the names of the files as they are archived.
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    94
   * -f mentioning the file name of the resulting tar archive.
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    95
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    96
To create archive of folder itself try this: ::
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    97
   
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    98
   $ tar -cvf fortune.tar fortune/
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    99
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   100
To add files to existing tar archive, option *`r`* is used: ::
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   101
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   102
   $ fortune > fourth.txt
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   103
   $ tar -r fourth.txt -vf allfiles.tar
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   104
   fourth.txt
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   105
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   106
There are other options too available for explicitly mentioning the position of archive, use *tar --help* for getting all the details.
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   107
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   108
Similarly to remove file from archive use *--delete* option: ::
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   109
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   110
   $ tar --delete second.txt -f allfiles.tar
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   111
   $ tar -tf allfiles.tar
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   112
   first.txt
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   113
   third.txt
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   114
   fourth.txt
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   115
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   116
Listing the files of archive:
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   117
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   118
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   119
Once files are archived, tar command have the *`t`* option, for Listing all files in the tar file: ::
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   120
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   121
   $ tar tf allfiles.tar
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   122
   first.txt
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   123
   second.txt
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   124
   third.txt
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   125
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   126
**//this is not working for me in some cases :(**
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   127
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   128
To locate a particular file among the archive mention its name after *t* option. ::
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   129
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   130
   $ tar t second.txt allfiles.tar
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   131
   second.txt
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   132
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   133
one can also use elementary regex for locating the file, so in previous case even second.* will also return the same result.
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   134
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   135
Extracting files from archive:
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   136
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   137
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   138
To extract the content from a archive, use *`x`* option: ::
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   139
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   140
   $ mkdir extract
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   141
   $ cp allfiles.tar extract/
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   142
   $ cd extract
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   143
   $ tar -xvf allfiles.tar 
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   144
   first.txt
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   145
   second.txt
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   146
   third.txt
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   147
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   148
To extract any particular file from archive, mention the name of file after *x* option: ::
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   149
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   150
   $ tar -x second.txt -vf allfiles.tar 
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   151
   second.txt
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   152
   
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   153
    
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   154
Further Reading for this section:
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   155
---------------------------------	
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   156
	
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   157
	* http://en.wikipedia.org/wiki/Tar_(file_format)
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   158
	* http://www.gnu.org/software/tar/manual/tar.html 
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   159
	* http://linuxreviews.org/beginner/
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   160
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   161
GZip:
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   162
=====
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   163
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   164
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 *gzip* 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 *replace the existing file*.
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   165
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   166
Get going:
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   167
----------
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   168
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   169
As usual first commands to check out with gzip are *man* and *help*, ::
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   170
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   171
    $ man gzip
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   172
    $ gzip --help
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   173
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   174
Creating a zip of a bunch of files is fairly trivial, it can be done simply via: ::
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   175
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   176
    $ gzip [OPTION]... [FILE]...
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   177
    
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   178
Creating zip files:
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   179
~~~~~~~~~~~~~~~~~~~
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   180
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   181
Continuing from previous set of files and setup, we will like to zip them and hence the command would be: ::
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   182
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   183
    $ gzip first.txt fourth.txt second.txt third.txt
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   184
    $ ls
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   185
    allfiles.tar  first.txt.gz  fourth.txt.gz  second.txt.gz  third.txt.gz  zipped.tar.gz
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   186
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   187
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: ::
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   188
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   189
    $ gzip *.txt
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   190
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   191
Similar to *tar* command, one can also use *`-v`* option here to see the output in *verbose* form. For the previous example, if we enable this option the result would be something like this: ::
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   192
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   193
    $ gzip -v *.txt
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   194
    first.txt:	  4.4% -- replaced with first.txt.gz
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   195
    fourth.txt:	 -7.1% -- replaced with fourth.txt.gz
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   196
    second.txt:	 -4.8% -- replaced with second.txt.gz
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   197
    third.txt:	  3.8% -- replaced with third.txt.gz    
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   198
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   199
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 *pipes* and *redirections* ::
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   200
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   201
    $ gzip -c *.txt > all.gz 
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   202
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   203
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 *`-c`* option states to print the output to standard output(stdout) and following *`>`* 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. 
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   204
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   205
For creating a zip archive of a complete directory, one has to use *`-r`* 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 : ::
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   206
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   207
    $ gzip -r fortune-files/
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   208
    $ gzip -rv .
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   209
    ./first.txt:	  4.4% -- replaced with ./first.txt.gz
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   210
    ./second.txt:	 -4.8% -- replaced with ./second.txt.gz
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   211
    ./third.txt:	  3.8% -- replaced with ./third.txt.gz
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   212
    ./allfiles.tar:	 96.6% -- replaced with ./allfiles.tar.gz
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   213
    ./fourth.txt:	 -7.1% -- replaced with ./fourth.txt.gz
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   214
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   215
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 *`g`*. ::
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   216
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   217
    $ tar -cvzf zipped.tar.gz *.txt 
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   218
    first.txt
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   219
    fourth.txt
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   220
    second.txt
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   221
    third.txt
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   222
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   223
*Thats why gzip is designed as a complement to tar, not as a replacement.*
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   224
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   225
gzip command comes with a option *`-l`* to view the compressed file contents: ::
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   226
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   227
    $ gzip -l zipped.tar.gz
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   228
             compressed        uncompressed  ratio uncompressed_name
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   229
                332               10240      97.0% zipped.tar
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   230
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   231
Other feature of gzip is option for mentioning the kind of compression one wants. There is a option of *`-n`* where *n varies from 0 to 9* which regulate the speed/quality of compression. With *`-1`* or *`--fast`* option it means the fastest compression method (less compression) and *`--best`* or *`-9`* indicates the slowest compression method, default compression level is *`-6`*.
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   232
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   233
To decompress a already compressed file there are two options, either use *`gunzip`* command or use *`-d`* option with gzip command: ::
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   234
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   235
    $ gzip -dv *.gz 
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   236
    all.gz:	-440.4% -- replaced with all
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   237
    first.txt.gz:	  4.4% -- replaced with first.txt
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   238
    fourth.txt.gz:	 -7.1% -- replaced with fourth.txt
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   239
    second.txt.gz:	 -4.8% -- replaced with second.txt
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   240
    third.txt.gz:	  3.8% -- replaced with third.txt
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   241
    zipped.tar.gz:	 97.0% -- replaced with zipped.tar
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   242
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   243
or: ::
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   244
    
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   245
    $ gunzip -v *.gz
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   246
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   247
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 *tar*.
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   248
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   249
Further Reading for this section:
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   250
---------------------------------
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   251
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   252
	* http://linuxreviews.org/beginner/
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   253
	* http://lowfatlinux.com/linux-gzip-gunzip.html
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   254
	* http://www.gnu.org/software/gzip/manual/gzip.html
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   255
	* http://en.wikipedia.org/wiki/ZIP_(file_format)
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   256
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   257
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   258
File Comparisons:
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   259
=================
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   260
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   261
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: 
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   262
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   263
cmp:
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   264
----
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   265
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   266
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: ::
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   267
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   268
   $ find . -name quick.c
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   269
   ./Desktop/programs/quick.c
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   270
   ./c-folder/quick.c
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   271
   $ cmp Desktop/programs/quick.c c-folder/quick.c
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   272
   $
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   273
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   274
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: ::
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   275
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   276
   $ cmp Desktop/programs/quick.c c-folder/quick.c
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   277
   Desktop/programs/quick.c c-folder/quick.c differ: byte 339, line 24
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   278
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   279
That is, if files differ, the byte and line number at which the first difference occurred is reported. 
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   280
 
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   281
diff:
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   282
-----
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   283
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   284
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: ::
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   285
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   286
    $ echo -e "quick\nbrown\nfox\njumped\nover\nthe\nlazy\ndog" > allcharacters.txt
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   287
    $ echo -e "quick\nbrown\nfox\njmuped\nover\nteh\nlzay\ndog" > problem.txt
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   288
    $ diff problem.txt allcharacters.txt 
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   289
    4c4
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   290
    < jmuped
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   291
    ---
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   292
    > jumped
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   293
    6,7c6,7
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   294
    < teh
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   295
    < lzay
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   296
    ---
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   297
    > the
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   298
    > lazy
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   299
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   300
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 *sdiff*, for the same files using sdiff will result in: ::
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   301
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   302
    $ sdiff problem.txt allcharacters.txt 
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   303
    quick								quick
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   304
    brown								brown
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   305
    fox									fox
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   306
    jmuped							      |	jumped
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   307
    over								over
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   308
    teh								      |	the
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   309
    lzay							      |	lazy
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   310
    dog								      	dog   
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   311
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   312
Some exercise for a change:
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   313
 
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   314
     * Try using diff for any binary file, does it work? 
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   315
     * What are other equivalent for diff command based on needs/requirements?
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   316
     * Can we use diff to compare two directories? If yes how?
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   317
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   318
comm:
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   319
-----
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   320
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   321
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: ::
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   322
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   323
   $ sort allcharacters.txt>sortedcharac.txt; sort problem.txt>sortedprob.txt
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   324
   $ comm sortedcharac.txt sortedprob.txt 
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   325
		brown
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   326
		dog
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   327
		fox
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   328
	jmuped
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   329
   jumped
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   330
   lazy
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   331
	lzay
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   332
		over
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   333
		quick
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   334
	teh
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   335
   the
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   336
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   337
Environment Variables:
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   338
======================
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   339
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   340
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.  
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   341
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   342
Some of examples of Environment variables are(result may vary!): ::
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   343
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   344
   $ echo $OSTYPE 
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   345
   linux-gnu
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   346
   $ echo $HOME
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   347
   /home/baali 
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   348
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   349
To see all the variables and there values use any of following commands: ::
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   350
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   351
   $ printenv | less
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   352
   $ env
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   353
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   354
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: ::
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   355
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   356
   $ set PATH=$PATH:$HOME/bin
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   357
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   358
See the difference in value of PATH variable before and after modifying it. One can also create its own variable to make things easier: ::
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   359
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   360
   $ set repo = $HOME/Desktop/random/code
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   361
   $ cd $repo
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   362
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   363
*set* 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 *export* them. Repeat the above mentioned activity with *export* command. Now with all new shells, *$repo* will work.
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   364
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   365
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 *.bashrc* file. 
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   366
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   367
Further Reading:
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   368
----------------
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   369
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   370
	* http://lowfatlinux.com/linux-environment-variables.html
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   371
	* http://www.codecoffee.com/tipsforlinux/articles/030.html
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   372
	* http://www.ee.surrey.ac.uk/Teaching/Unix/unix8.html
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   373
	* http://en.wikipedia.org/wiki/Environment_variable	
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   374
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   375
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   376
Shell Scripting:
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   377
================
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   378
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   379
Basics:
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   380
-------
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   381
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   382
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 *"Hello World"* sample for shell scripting is as easy as it sounds: ::
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   383
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   384
   $ echo '#!/bin/sh' > my-script.sh
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   385
   $ clear >> my-script.sh   
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   386
   $ echo 'echo Hello World' >> my-script.sh
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   387
   $ chmod 755 my-script.sh
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   388
   $ ./my-script.sh
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   389
   Hello World
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   390
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   391
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, *clear* and *echo* one after another. One can also do the same task using a one liner command *clear; echo 'Hello World';* but as number of lines grows using a script file is helpful. 
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   392
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   393
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 *initial.sh*, open it with text editor, and write: ::
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   394
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   395
   #!/bin/sh
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   396
   ls > temp
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   397
   grep ^$1 < temp
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   398
   rm temp
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   399
   $ chmod a+x initial.sh
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   400
   $ ./initial.sh s
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   401
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   402
The $1 in the script is pertaining to command line argument. All arguments passed via command line are accessed via *$#* 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: ::
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   403
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   404
   #!/bin/sh
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   405
   name=`find . -name $1 -print`
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   406
   echo $name
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   407
   last_modified=`stat -c %y $name| cut -f 1 -d " "`
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   408
   echo "Last modified: $last_modified"    
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   409
   $ ./search.sh fname
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   410
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   411
Try giving some file you want to search in place of fname. Please note in second line *`* 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 *$* symbol before name of variable.
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   412
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   413
naming conventions for variables?? do we need them??
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   414
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   415
Shell Arithmetic:
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   416
-----------------
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   417
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   418
Shell also provides support for basic arithmetic operations. The syntax is: ::
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   419
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   420
   $ expr op1 math-operator op2
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   421
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   422
Some of example which can be tried handily: ::
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   423
   
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   424
   $ expr -3 + 5
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   425
   2
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   426
   $ expr 10 % 3
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   427
   1
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   428
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   429
These spaces in between operator and operands is important, without them shell interpreter will raise the syntax error. ::
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   430
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   431
   $ expr 2*3
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   432
   expr: syntax error
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   433
   
60
8a36825e21c5 Arithmetic section, typo correction, eg script.
Shantanu <shantanu@fossee.in>
parents: 59
diff changeset
   434
One can use back-quotes(`) also to get value of expr. ::
54
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   435
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   436
   $ echo `expr 6 + 3`
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   437
   9
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   438
   $ result=`expr 6 + 3`
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   439
   $ echo $result
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   440
   9
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   441
60
8a36825e21c5 Arithmetic section, typo correction, eg script.
Shantanu <shantanu@fossee.in>
parents: 59
diff changeset
   442
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. ::
54
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   443
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   444
   $ echo "Today is date"
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   445
   Today is date
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   446
   $ echo "Today is `date`"
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   447
   Today is Wed Sep 16 17:32:22 IST 2009
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   448
   $ echo 'Today is `date`'
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   449
   Today is `date`
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   450
   $ echo "Today is \n `date`"
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   451
   Today is \n Wed Sep 16 17:40:13 IST 2009
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   452
   $ echo -e "Today is \n `date`"
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   453
   Today is 
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   454
    Wed Sep 16 17:41:13 IST 2009 
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   455
60
8a36825e21c5 Arithmetic section, typo correction, eg script.
Shantanu <shantanu@fossee.in>
parents: 59
diff changeset
   456
if else construct:
8a36825e21c5 Arithmetic section, typo correction, eg script.
Shantanu <shantanu@fossee.in>
parents: 59
diff changeset
   457
------------------
54
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   458
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   459
One can have simple *if else if* 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: ::
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   460
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   461
   #!/bin/sh
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   462
   if test $1 -gt 0
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   463
   then
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   464
     echo "number is positive"
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   465
   else
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   466
     echo "number is negative"
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   467
   fi
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   468
   $ ./sign.sh -11
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   469
   number is negative
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   470
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   471
This script will compare the first value passed as argument with 0 *if test var -gt val*, 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 *if-else* clause which will warn user of correct usage of script. ::
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   472
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   473
   #this is the case when no argument is passed  
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   474
   if [ $# -eq 0 ]
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   475
   then
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   476
     echo "$0 : You must give/supply one integers"
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   477
     exit 1
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   478
   else 
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   479
     if [ $# -gt 1 ]
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   480
     then
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   481
       echo "$0 : You must give one integer"
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   482
       exit 1
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   483
     fi
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   484
   fi
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   485
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   486
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 *[ $# -eq 0 ]* will work properly, but if we remove those leading or trailing spaces like *[ $# -eq 0]*, it wont work as expected, or rather throw a warning. Both *test* and *[]* do the same task of testing a expression and returning true or false.
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   487
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   488
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 *.bashrc* file with *./greet.sh* line and we are done. The script is: ::
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   489
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   490
   #!/bin/sh
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   491
   #Script to greet the user according to time of day
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   492
   temph=`date | cut -c12-13`
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   493
   dat=`date +"%A %d in %B of %Y (%r)"`
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   494
   if [ $temph -lt 12 ]
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   495
   then
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   496
     mess="Good Morning $LOGNAME, Have a nice day!"
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   497
   fi
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   498
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   499
   if [ $temph -gt 12 -a $temph -le 16 ]
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   500
   then
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   501
     mess="Good Afternoon $LOGNAME"
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   502
   fi
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   503
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   504
   if [ $temph -gt 16 -a $temph -le 18 ]
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   505
   then
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   506
     mess="Good Evening $LOGNAME"
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   507
   fi
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   508
   echo -e "$mess\nThis is $dat"
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   509
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   510
For me when I open the shell the output is something like: ::
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   511
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   512
   Good Morning baali, Have a nice day!
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   513
   This is Wednesday 16 in September of 2009 (11:54:47 AM IST) 
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   514
56
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   515
Loops
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   516
-----
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   517
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   518
Bash has three different commands for looping -- ``for``, ``while`` and ``until``. 
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   519
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   520
``for`` loop
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   521
~~~~~~~~~~~~
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   522
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   523
Suppose we have a set of files, that have names beginning with numbers followed by their names - ``08 - Society.mp3``. 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 ``for`` loop, to loop through the list of files and rename each of the files.  
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   524
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   525
Let's first look at a simple ``for`` loop, to understand how it works. 
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   526
::
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   527
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   528
  for animal in rat cat dog man
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   529
  do 
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   530
    echo $animal
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   531
  done
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   532
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   533
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 ``animal`` is a dummy variable and has no significance. You could use something as lame as ``i`` in place of ``animal``.  
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   534
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   535
Now, we use a simple ``for`` loop to list the files that we are interested in. 
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   536
::
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   537
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   538
  ls *.mp3 > list
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   539
  for i in `cat list`
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   540
  do
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   541
    echo "$i"
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   542
  done
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   543
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   544
If your filenames contain spaces, ``for`` 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. 
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   545
::
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   546
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   547
  for i in *.mp3
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   548
  do
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   549
    echo "$i"
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   550
  done
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   551
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   552
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. 
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   553
::
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   554
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   555
  for i in *.mp3
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   556
  do 
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   557
    j=$(echo "$i"|grep -o "[A-Za-z'&. ]*.mp3")
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   558
    echo "$i -> $j"
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   559
  done
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   560
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   561
Now we just replace the echo command with a ``mv`` or a ``cp`` command. 
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   562
::
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   563
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   564
  for i in *.mp3
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   565
  do 
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   566
    j=$(echo "$i"|grep -o "[A-Za-z'&. ]*.mp3")
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   567
    cp "$i" "$j"
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   568
  done
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   569
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   570
As an exercise, you could try sorting the files in reverse alphabetical order and then prefix numbers to each of the filenames.  
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   571
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   572
``while``
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   573
~~~~~~~~~
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   574
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   575
The ``while`` command allows us to continuously execute a block of commands until the command that is controlling the loop is executing successfully. 
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   576
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   577
Let's start with the lamest example of a while loop.
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   578
::
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   579
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   580
  while true
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   581
  do
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   582
    echo "True"
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   583
  done
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   584
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   585
This, as you can see, is an infinite loop that prints the ``True``. 
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   586
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   587
Say we wish to write a simple program that takes user input and prints it back, until the input is ``quit``, which quits the program. 
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   588
::
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   589
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   590
  while [ "$variable" != "quit" ]
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   591
  do
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   592
    read variable
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   593
    echo "Input - $variable"
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   594
  done
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   595
  exit 0
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   596
60
8a36825e21c5 Arithmetic section, typo correction, eg script.
Shantanu <shantanu@fossee.in>
parents: 59
diff changeset
   597
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: ::
8a36825e21c5 Arithmetic section, typo correction, eg script.
Shantanu <shantanu@fossee.in>
parents: 59
diff changeset
   598
8a36825e21c5 Arithmetic section, typo correction, eg script.
Shantanu <shantanu@fossee.in>
parents: 59
diff changeset
   599
  #!/bin/sh
8a36825e21c5 Arithmetic section, typo correction, eg script.
Shantanu <shantanu@fossee.in>
parents: 59
diff changeset
   600
8a36825e21c5 Arithmetic section, typo correction, eg script.
Shantanu <shantanu@fossee.in>
parents: 59
diff changeset
   601
  #To check number of arguments being passed.
8a36825e21c5 Arithmetic section, typo correction, eg script.
Shantanu <shantanu@fossee.in>
parents: 59
diff changeset
   602
  if [ $# -eq 0 ] ; then
8a36825e21c5 Arithmetic section, typo correction, eg script.
Shantanu <shantanu@fossee.in>
parents: 59
diff changeset
   603
  echo "Correct usage: $0 tar-archive filename \nOr $0 filename"
8a36825e21c5 Arithmetic section, typo correction, eg script.
Shantanu <shantanu@fossee.in>
parents: 59
diff changeset
   604
  exit 1
8a36825e21c5 Arithmetic section, typo correction, eg script.
Shantanu <shantanu@fossee.in>
parents: 59
diff changeset
   605
  else
8a36825e21c5 Arithmetic section, typo correction, eg script.
Shantanu <shantanu@fossee.in>
parents: 59
diff changeset
   606
    if [ $# -eq 1 ] ; then
8a36825e21c5 Arithmetic section, typo correction, eg script.
Shantanu <shantanu@fossee.in>
parents: 59
diff changeset
   607
      tar_archive=`find $PWD -name "*.tar*"`
8a36825e21c5 Arithmetic section, typo correction, eg script.
Shantanu <shantanu@fossee.in>
parents: 59
diff changeset
   608
    else
8a36825e21c5 Arithmetic section, typo correction, eg script.
Shantanu <shantanu@fossee.in>
parents: 59
diff changeset
   609
      tar_archive=`find $PWD -name $1`
8a36825e21c5 Arithmetic section, typo correction, eg script.
Shantanu <shantanu@fossee.in>
parents: 59
diff changeset
   610
    fi
8a36825e21c5 Arithmetic section, typo correction, eg script.
Shantanu <shantanu@fossee.in>
parents: 59
diff changeset
   611
  fi
8a36825e21c5 Arithmetic section, typo correction, eg script.
Shantanu <shantanu@fossee.in>
parents: 59
diff changeset
   612
8a36825e21c5 Arithmetic section, typo correction, eg script.
Shantanu <shantanu@fossee.in>
parents: 59
diff changeset
   613
  #Search of particular file inside archives.
8a36825e21c5 Arithmetic section, typo correction, eg script.
Shantanu <shantanu@fossee.in>
parents: 59
diff changeset
   614
  for archive in $tar_archive
8a36825e21c5 Arithmetic section, typo correction, eg script.
Shantanu <shantanu@fossee.in>
parents: 59
diff changeset
   615
  do
8a36825e21c5 Arithmetic section, typo correction, eg script.
Shantanu <shantanu@fossee.in>
parents: 59
diff changeset
   616
    echo $archive
8a36825e21c5 Arithmetic section, typo correction, eg script.
Shantanu <shantanu@fossee.in>
parents: 59
diff changeset
   617
    variable=`tar -tf $archive`
8a36825e21c5 Arithmetic section, typo correction, eg script.
Shantanu <shantanu@fossee.in>
parents: 59
diff changeset
   618
    for word in $variable
8a36825e21c5 Arithmetic section, typo correction, eg script.
Shantanu <shantanu@fossee.in>
parents: 59
diff changeset
   619
    do
8a36825e21c5 Arithmetic section, typo correction, eg script.
Shantanu <shantanu@fossee.in>
parents: 59
diff changeset
   620
      if [ $# -eq 1 ] ; then
8a36825e21c5 Arithmetic section, typo correction, eg script.
Shantanu <shantanu@fossee.in>
parents: 59
diff changeset
   621
        echo "$word" | grep -q ".*$1"
8a36825e21c5 Arithmetic section, typo correction, eg script.
Shantanu <shantanu@fossee.in>
parents: 59
diff changeset
   622
      else
8a36825e21c5 Arithmetic section, typo correction, eg script.
Shantanu <shantanu@fossee.in>
parents: 59
diff changeset
   623
	echo "$word" | grep -q ".*$2"
8a36825e21c5 Arithmetic section, typo correction, eg script.
Shantanu <shantanu@fossee.in>
parents: 59
diff changeset
   624
      fi
8a36825e21c5 Arithmetic section, typo correction, eg script.
Shantanu <shantanu@fossee.in>
parents: 59
diff changeset
   625
    if [ $? -eq 0 ] ; then 
8a36825e21c5 Arithmetic section, typo correction, eg script.
Shantanu <shantanu@fossee.in>
parents: 59
diff changeset
   626
      echo "File present in $archive!" 
8a36825e21c5 Arithmetic section, typo correction, eg script.
Shantanu <shantanu@fossee.in>
parents: 59
diff changeset
   627
    fi  
8a36825e21c5 Arithmetic section, typo correction, eg script.
Shantanu <shantanu@fossee.in>
parents: 59
diff changeset
   628
    done
8a36825e21c5 Arithmetic section, typo correction, eg script.
Shantanu <shantanu@fossee.in>
parents: 59
diff changeset
   629
  done
8a36825e21c5 Arithmetic section, typo correction, eg script.
Shantanu <shantanu@fossee.in>
parents: 59
diff changeset
   630
56
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   631
``until``
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   632
~~~~~~~~~
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   633
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   634
The ``until`` loop is similar to the ``while`` loop, except that it executes until the conditional command does not execute properly. 
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   635
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   636
The infinite loop changes to the following, when ``until`` is used.
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   637
::
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   638
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   639
  until false
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   640
  do
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   641
    echo "True"
eee394eb05fc Added Loops section in ULT, modified LaTeX handout to use examples from sample doc.
Puneeth Chaganti <puneeth@fossee.in>
parents: 54
diff changeset
   642
  done
54
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   643
59
8c15077f028d Added a section on functions in ULT; minor edits to SciTE section.
Puneeth Chaganti <puneeth@fossee.in>
parents: 56
diff changeset
   644
Functions
8c15077f028d Added a section on functions in ULT; minor edits to SciTE section.
Puneeth Chaganti <puneeth@fossee.in>
parents: 56
diff changeset
   645
---------
8c15077f028d Added a section on functions in ULT; minor edits to SciTE section.
Puneeth Chaganti <puneeth@fossee.in>
parents: 56
diff changeset
   646
8c15077f028d Added a section on functions in ULT; minor edits to SciTE section.
Puneeth Chaganti <puneeth@fossee.in>
parents: 56
diff changeset
   647
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, ``hello-world``. Functions can be defined in bash, either using the ``function`` built-in followed by the function name or just the function name followed by a pair of parentheses. 
8c15077f028d Added a section on functions in ULT; minor edits to SciTE section.
Puneeth Chaganti <puneeth@fossee.in>
parents: 56
diff changeset
   648
::
8c15077f028d Added a section on functions in ULT; minor edits to SciTE section.
Puneeth Chaganti <puneeth@fossee.in>
parents: 56
diff changeset
   649
8c15077f028d Added a section on functions in ULT; minor edits to SciTE section.
Puneeth Chaganti <puneeth@fossee.in>
parents: 56
diff changeset
   650
  function hello-world
8c15077f028d Added a section on functions in ULT; minor edits to SciTE section.
Puneeth Chaganti <puneeth@fossee.in>
parents: 56
diff changeset
   651
  { 
8c15077f028d Added a section on functions in ULT; minor edits to SciTE section.
Puneeth Chaganti <puneeth@fossee.in>
parents: 56
diff changeset
   652
  echo "Hello, World."; 
8c15077f028d Added a section on functions in ULT; minor edits to SciTE section.
Puneeth Chaganti <puneeth@fossee.in>
parents: 56
diff changeset
   653
  }
8c15077f028d Added a section on functions in ULT; minor edits to SciTE section.
Puneeth Chaganti <puneeth@fossee.in>
parents: 56
diff changeset
   654
8c15077f028d Added a section on functions in ULT; minor edits to SciTE section.
Puneeth Chaganti <puneeth@fossee.in>
parents: 56
diff changeset
   655
  hello-world () {
8c15077f028d Added a section on functions in ULT; minor edits to SciTE section.
Puneeth Chaganti <puneeth@fossee.in>
parents: 56
diff changeset
   656
    echo "Hello, World.";
8c15077f028d Added a section on functions in ULT; minor edits to SciTE section.
Puneeth Chaganti <puneeth@fossee.in>
parents: 56
diff changeset
   657
  }
8c15077f028d Added a section on functions in ULT; minor edits to SciTE section.
Puneeth Chaganti <puneeth@fossee.in>
parents: 56
diff changeset
   658
8c15077f028d Added a section on functions in ULT; minor edits to SciTE section.
Puneeth Chaganti <puneeth@fossee.in>
parents: 56
diff changeset
   659
  $ hello-world
8c15077f028d Added a section on functions in ULT; minor edits to SciTE section.
Puneeth Chaganti <puneeth@fossee.in>
parents: 56
diff changeset
   660
  Hello, World.
8c15077f028d Added a section on functions in ULT; minor edits to SciTE section.
Puneeth Chaganti <puneeth@fossee.in>
parents: 56
diff changeset
   661
8c15077f028d Added a section on functions in ULT; minor edits to SciTE section.
Puneeth Chaganti <puneeth@fossee.in>
parents: 56
diff changeset
   662
Passing parameters to functions is similar to passing them to scripts. 
8c15077f028d Added a section on functions in ULT; minor edits to SciTE section.
Puneeth Chaganti <puneeth@fossee.in>
parents: 56
diff changeset
   663
::
8c15077f028d Added a section on functions in ULT; minor edits to SciTE section.
Puneeth Chaganti <puneeth@fossee.in>
parents: 56
diff changeset
   664
8c15077f028d Added a section on functions in ULT; minor edits to SciTE section.
Puneeth Chaganti <puneeth@fossee.in>
parents: 56
diff changeset
   665
  function hello-name
8c15077f028d Added a section on functions in ULT; minor edits to SciTE section.
Puneeth Chaganti <puneeth@fossee.in>
parents: 56
diff changeset
   666
  { 
8c15077f028d Added a section on functions in ULT; minor edits to SciTE section.
Puneeth Chaganti <puneeth@fossee.in>
parents: 56
diff changeset
   667
  echo "Hello, $1."; 
8c15077f028d Added a section on functions in ULT; minor edits to SciTE section.
Puneeth Chaganti <puneeth@fossee.in>
parents: 56
diff changeset
   668
  }
8c15077f028d Added a section on functions in ULT; minor edits to SciTE section.
Puneeth Chaganti <puneeth@fossee.in>
parents: 56
diff changeset
   669
8c15077f028d Added a section on functions in ULT; minor edits to SciTE section.
Puneeth Chaganti <puneeth@fossee.in>
parents: 56
diff changeset
   670
  $ hello-name 9
8c15077f028d Added a section on functions in ULT; minor edits to SciTE section.
Puneeth Chaganti <puneeth@fossee.in>
parents: 56
diff changeset
   671
  Hello, 9.
8c15077f028d Added a section on functions in ULT; minor edits to SciTE section.
Puneeth Chaganti <puneeth@fossee.in>
parents: 56
diff changeset
   672
8c15077f028d Added a section on functions in ULT; minor edits to SciTE section.
Puneeth Chaganti <puneeth@fossee.in>
parents: 56
diff changeset
   673
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 ``local`` built-in command of bash.
8c15077f028d Added a section on functions in ULT; minor edits to SciTE section.
Puneeth Chaganti <puneeth@fossee.in>
parents: 56
diff changeset
   674
8c15077f028d Added a section on functions in ULT; minor edits to SciTE section.
Puneeth Chaganti <puneeth@fossee.in>
parents: 56
diff changeset
   675
We shall now write a function for the word frequency generating script that we had looked at in the previous session. 
8c15077f028d Added a section on functions in ULT; minor edits to SciTE section.
Puneeth Chaganti <puneeth@fossee.in>
parents: 56
diff changeset
   676
8c15077f028d Added a section on functions in ULT; minor edits to SciTE section.
Puneeth Chaganti <puneeth@fossee.in>
parents: 56
diff changeset
   677
::
8c15077f028d Added a section on functions in ULT; minor edits to SciTE section.
Puneeth Chaganti <puneeth@fossee.in>
parents: 56
diff changeset
   678
8c15077f028d Added a section on functions in ULT; minor edits to SciTE section.
Puneeth Chaganti <puneeth@fossee.in>
parents: 56
diff changeset
   679
  function word_frequency {
8c15077f028d Added a section on functions in ULT; minor edits to SciTE section.
Puneeth Chaganti <puneeth@fossee.in>
parents: 56
diff changeset
   680
    if [ $# -ne 1 ]
8c15077f028d Added a section on functions in ULT; minor edits to SciTE section.
Puneeth Chaganti <puneeth@fossee.in>
parents: 56
diff changeset
   681
    then
8c15077f028d Added a section on functions in ULT; minor edits to SciTE section.
Puneeth Chaganti <puneeth@fossee.in>
parents: 56
diff changeset
   682
      echo "Usage: $0 file_name"
8c15077f028d Added a section on functions in ULT; minor edits to SciTE section.
Puneeth Chaganti <puneeth@fossee.in>
parents: 56
diff changeset
   683
      exit 1
8c15077f028d Added a section on functions in ULT; minor edits to SciTE section.
Puneeth Chaganti <puneeth@fossee.in>
parents: 56
diff changeset
   684
    else 
8c15077f028d Added a section on functions in ULT; minor edits to SciTE section.
Puneeth Chaganti <puneeth@fossee.in>
parents: 56
diff changeset
   685
      if [ -f "$1" ]
8c15077f028d Added a section on functions in ULT; minor edits to SciTE section.
Puneeth Chaganti <puneeth@fossee.in>
parents: 56
diff changeset
   686
      then
8c15077f028d Added a section on functions in ULT; minor edits to SciTE section.
Puneeth Chaganti <puneeth@fossee.in>
parents: 56
diff changeset
   687
        grep  "[A-Za-z]*" -o "$1" | tr 'A-Z' 'a-z' | sort | uniq -c | sort -nr | less
8c15077f028d Added a section on functions in ULT; minor edits to SciTE section.
Puneeth Chaganti <puneeth@fossee.in>
parents: 56
diff changeset
   688
      fi
8c15077f028d Added a section on functions in ULT; minor edits to SciTE section.
Puneeth Chaganti <puneeth@fossee.in>
parents: 56
diff changeset
   689
    fi
8c15077f028d Added a section on functions in ULT; minor edits to SciTE section.
Puneeth Chaganti <puneeth@fossee.in>
parents: 56
diff changeset
   690
  }
8c15077f028d Added a section on functions in ULT; minor edits to SciTE section.
Puneeth Chaganti <puneeth@fossee.in>
parents: 56
diff changeset
   691
8c15077f028d Added a section on functions in ULT; minor edits to SciTE section.
Puneeth Chaganti <puneeth@fossee.in>
parents: 56
diff changeset
   692
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).
8c15077f028d Added a section on functions in ULT; minor edits to SciTE section.
Puneeth Chaganti <puneeth@fossee.in>
parents: 56
diff changeset
   693
54
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   694
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   695
Further Reading:
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   696
---------------- 
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   697
	* http://www.freeos.com/guides/lsst/ 
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   698
	* http://bash.cyberciti.biz/guide/Main_Page
60
8a36825e21c5 Arithmetic section, typo correction, eg script.
Shantanu <shantanu@fossee.in>
parents: 59
diff changeset
   699
	* http://tldp.org/LDP/abs/html/
8a36825e21c5 Arithmetic section, typo correction, eg script.
Shantanu <shantanu@fossee.in>
parents: 59
diff changeset
   700
	* http://tldp.org/LDP/Bash-Beginners-Guide/html/Bash-Beginners-Guide.html
54
bed3fe467e21 First version of 5-6 Session of ult.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   701
	
60
8a36825e21c5 Arithmetic section, typo correction, eg script.
Shantanu <shantanu@fossee.in>
parents: 59
diff changeset
   702
8a36825e21c5 Arithmetic section, typo correction, eg script.
Shantanu <shantanu@fossee.in>
parents: 59
diff changeset
   703
..  LocalWords:  allfiles txt cvf vf tf regex mkdir cp cd xvf gzip gz stdout