ult/Section_5.rst
changeset 60 8a36825e21c5
parent 59 8c15077f028d
child 63 dc13f22e9489
equal deleted inserted replaced
59:8c15077f028d 60:8a36825e21c5
     9     * What are Shell Scripts					      U
     9     * What are Shell Scripts					      U
    10   - Able to use file comparison commands like 			      Ap
    10   - Able to use file comparison commands like 			      Ap
    11     diff, cmp, comm
    11     diff, cmp, comm
    12   - Create and extract archives(.tar files) and zipped files(.gz)     Ap
    12   - Create and extract archives(.tar files) and zipped files(.gz)     Ap
    13   - Set/Modify environment as per need	    	                      Ap
    13   - Set/Modify environment as per need	    	                      Ap
    14   - Create shell scripts to autmoate tasks.			      Ap
    14   - Create shell scripts to automate tasks.			      Ap
    15 
    15 
    16 tar:
    16 tar:
    17 ====
    17 ====
    18 
    18 
    19 Introduction:
    19 Introduction:
   429 These spaces in between operator and operands is important, without them shell interpreter will raise the syntax error. ::
   429 These spaces in between operator and operands is important, without them shell interpreter will raise the syntax error. ::
   430 
   430 
   431    $ expr 2*3
   431    $ expr 2*3
   432    expr: syntax error
   432    expr: syntax error
   433    
   433    
   434 One can use backquotes(`) also to get value of expr. ::
   434 One can use back-quotes(`) also to get value of expr. ::
   435 
   435 
   436    $ echo `expr 6 + 3`
   436    $ echo `expr 6 + 3`
   437    9
   437    9
   438    $ result=`expr 6 + 3`
   438    $ result=`expr 6 + 3`
   439    $ echo $result
   439    $ echo $result
   440    9
   440    9
   441 
   441 
   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 enclsed within them is just same, no formulation/interpretaion. Back quotes(`), anything inclosed is considered as command, or is executed. ::
   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. ::
   443 
   443 
   444    $ echo "Today is date"
   444    $ echo "Today is date"
   445    Today is date
   445    Today is date
   446    $ echo "Today is `date`"
   446    $ echo "Today is `date`"
   447    Today is Wed Sep 16 17:32:22 IST 2009
   447    Today is Wed Sep 16 17:32:22 IST 2009
   451    Today is \n Wed Sep 16 17:40:13 IST 2009
   451    Today is \n Wed Sep 16 17:40:13 IST 2009
   452    $ echo -e "Today is \n `date`"
   452    $ echo -e "Today is \n `date`"
   453    Today is 
   453    Today is 
   454     Wed Sep 16 17:41:13 IST 2009 
   454     Wed Sep 16 17:41:13 IST 2009 
   455 
   455 
   456 if construct:
   456 if else construct:
   457 -------------
   457 ------------------
   458 
   458 
   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: ::
   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: ::
   460 
   460 
   461    #!/bin/sh
   461    #!/bin/sh
   462    if test $1 -gt 0
   462    if test $1 -gt 0
   592     read variable
   592     read variable
   593     echo "Input - $variable"
   593     echo "Input - $variable"
   594   done
   594   done
   595   exit 0
   595   exit 0
   596 
   596 
       
   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: ::
       
   598 
       
   599   #!/bin/sh
       
   600 
       
   601   #To check number of arguments being passed.
       
   602   if [ $# -eq 0 ] ; then
       
   603   echo "Correct usage: $0 tar-archive filename \nOr $0 filename"
       
   604   exit 1
       
   605   else
       
   606     if [ $# -eq 1 ] ; then
       
   607       tar_archive=`find $PWD -name "*.tar*"`
       
   608     else
       
   609       tar_archive=`find $PWD -name $1`
       
   610     fi
       
   611   fi
       
   612 
       
   613   #Search of particular file inside archives.
       
   614   for archive in $tar_archive
       
   615   do
       
   616     echo $archive
       
   617     variable=`tar -tf $archive`
       
   618     for word in $variable
       
   619     do
       
   620       if [ $# -eq 1 ] ; then
       
   621         echo "$word" | grep -q ".*$1"
       
   622       else
       
   623 	echo "$word" | grep -q ".*$2"
       
   624       fi
       
   625     if [ $? -eq 0 ] ; then 
       
   626       echo "File present in $archive!" 
       
   627     fi  
       
   628     done
       
   629   done
       
   630 
   597 ``until``
   631 ``until``
   598 ~~~~~~~~~
   632 ~~~~~~~~~
   599 
   633 
   600 The ``until`` loop is similar to the ``while`` loop, except that it executes until the conditional command does not execute properly. 
   634 The ``until`` loop is similar to the ``while`` loop, except that it executes until the conditional command does not execute properly. 
   601 
   635 
   659 
   693 
   660 
   694 
   661 Further Reading:
   695 Further Reading:
   662 ---------------- 
   696 ---------------- 
   663 	* http://www.freeos.com/guides/lsst/ 
   697 	* http://www.freeos.com/guides/lsst/ 
   664 	* http://www.freeos.com/guides/lsst/ch02sec01.html
       
   665 	* http://bash.cyberciti.biz/guide/Main_Page
   698 	* http://bash.cyberciti.biz/guide/Main_Page
       
   699 	* http://tldp.org/LDP/abs/html/
       
   700 	* http://tldp.org/LDP/Bash-Beginners-Guide/html/Bash-Beginners-Guide.html
   666 	
   701 	
       
   702 
       
   703 ..  LocalWords:  allfiles txt cvf vf tf regex mkdir cp cd xvf gzip gz stdout