Tuesday, February 9, 2016

Introduction of UNIX Shell Scripting

************************ UNIX Shell Scripting ***************************************
1) It is not direct programming language.

2) It is use to interact with existing shell of Operating System.

3) Shell Script is consider as a normal text file which contains set of command which are pass  our running shell.

4) Shell Script are Operating System dependence means if we write a shell  script for Linux which is unable to run Windows platform.

5) Shell Script are not compile and interpreter files.

6) Shell Script gets passed in line by line manner.

7) Shell Script supports every command which is supported by our normal shell.

8) There are different programming paradigm is available for shell as
    i) We can generate output.
   ii) We can accept input.
   iii) We can declare variable.
   iv) We can execute an expression.
   v) We can write branch condition syntax (if-else).
   vi) We can write iterative syntax (loop).
  vii) We We can write your own user defined function.

There are different type of shells but there internal working is almost same.
The shell is supported by Operating System are present in /etc/shells file.

Generally we have to use bourne again shell(bash).
--------------------------------------------------------------------------------------------------------------------------

1) To write a shell script we have to create normal text file whose extension should be .sh

2) Inside that text file we can write any command or any shell syntax.

3) As shell scripts are not compiled we have to use the name of shell and pass name of our shell scripts.
e.g. $ sh demo.sh

By using this command our shell execute every command from demo.sh file.

4) If we want to display some thing on stdout (console) we have to use echo keyword
e.g. echo hello world!!!

5) We can also accept input from stdin (Keyboard) by using read keyword as
e.g. echo enter your age
       read age

In above syntax "enter your age " string gets printed on screen and we enter the number it gets accepted in age variable.

6) If we want to print contents of any variable then we have to write $ symbol before name of that variable.
e.g. echo your age is $age.

7) There is no concept of datatype in Shell Script due to which every data gets store in set of bytes.

8) Like a normal programming language we can use any name for our variable without any convention .

9) In Shell Script there is no need to declare the variable before its used.

10) We can also create the variable whose value is constant throughout the execution of Shell Script.
e.g. value = 11
       readonly value
      echo $value

11) We can normal programming language we can accept command line argument as
e.g. Consider name of Shell Script is demo.sh

echo demo of command line argument
echo Total number of argument are $#
echo First argument $1
echo Second argument $2

12) Like normal if-else condition we can use in a shell script as
e.g. if statement or expression
       then
              #code
       else
             #code
       fi

After if keyword we can use any of the statement or expression
Generally every internal command returns a boolean value as a status due to which we can use that command in if condition.

13) Like normal C programming loops we can write loop in our shell script
e.g. value = 0
      while [$value -lt 10]         # -lt - less than
      do
                echo Hello World
                $value = $value+1
      done

The condition that we want to check should be in square bracket . do keyword is used as open curly bracket and done keyword is used as close curly bracket.

We can write any linux in build command in it which can be supported by our shell.

All file handling process related task can be perform with the help of shell script.

There are some command which are dependent with currently running shell. In that case instead of this kind command we can used of any linux platform.

If which shell is to be selected is not provided then by default bash (bourne) shell is selected.

As shell script are not compile we have to check every command independently by using normal terminal.

If there are nth commands in shell script and if mth command generate problem in it then first mth commands executes sequentially and our shell script abort normal.

If our shell script contain such thing which require super user privilege then make that privilege at the start of shell script.

--------------------------------------------------------------------------------------------------------------------------

File Handling in Shell Script:

In our shell script we can write any command which handles file system.

By using a shell script we can manipulate file system by creating new directory file in it.

For file creation and directory creation it is necessary to switch at the particular path before executing any command.

--------------------------------------------------------------------------------------------------------------------------

Linear Piping- To give the output of one command to other command input.

It is require if there is we can use multiple pipe in sequence to get desire output.

                  e.g. ls | wc -l

Output of ls command is accepted by  wc command which count number of lines from the ls command output.

                e.g. ls -l | grep "^d" | wc -l

ls command gives the list of entry from our directory. that output is accepted as input of grep command which filter every such line which start with 'd'.

All this line pass to the input of wc command  which gives number of lines.

This while command gives number of directory in given path.

-------------------------------------------------------------------------------------------------------------------------

For loops in Shell Script:

Like normal for loop in C and C++. We can write a for loop in shell script also.

For that for loop we have to specify the condition like normal programming.
-------------------------------------------------------------------------------------------------------------------------

Accept string from user and search that string in every file.

echo Enter the string
read name

flag = 0

for file in *
do
           if[$flag -eq 0]
           then
                    if[-f $file]
                    then
                          grep $str $file
               
                          if[$? -eq 0]
                          then
                                flag = 1
                                 echo $file
                         fi
                   if
             fi
 done

--------------------------------------------------------------------------------------------------------------------------

In this shell script str contain the name of string we want to search

for loop is written with * as a regular expression

file is a keyword which give every from the directory.

By using -f option we are checking whether is regular file or not

If it is regular file we are using grep command to search that string in that file.

$? command gives exist status of previous command. If exist status is zero then that particular string is available in that file.

To stop that searching flag variable is use.
--------------------------------------------------------------------------------------------------------------------------

Command to print permission of all the files

For this we have to use ls command to apply the filter use grep command to get the specific number of values use cut command.  

By using cut command we can only fetch desire number of bytes from input.

e.g. echo Enter path of directory
       read path

ls -l $path | grep "^d" | cut -f1,9 -d ""

By using this command we can get the permission fields with the delimiter as space.

grep command is use to filter only directory name from the output generated by ls command.

As that output contain multiple field unit we have to break that field by using cut command.

cut -f1, 9 -d " "

cut - is the name of command.
-f - flag is used to get desire field from that input.
1 and 9- are consider as column number.
-d - is use to specify the delimiter which use to find out column length.

--------------------------------------------------------------------------------------------------------------------------

Expression evaluation in Shell Script: -

If we write any expression in a shell script then that expression should be under expr keyword .
We can perform every arithmetic operation by expr keyword.

The operation that we want perform should by arithmetic logical unit.

Due to this concept time which is evaluate the expression is less as compare to normal programming expression.

We can perform the operation as  +, -, *, /, %.

e.g. Number1 = 110
      Number2 = 120
      Number3 = `expr $Number1 + $number2`
     echo $Number3
--------------------------------------------------------------------------------------------------------------------------



No comments:

Post a Comment

SYBSc (CS) Sem III : Data Structure Slip 14 Que - 2

  SAVITIBAI PHULE UNIVERSITY OF PUNE S. Y. B.Sc. (Computer Science) Semester III Practical Examination       SUBJECT: CS-233 Practical cours...