Linux Shell And Shell Scripting
 
What is a shell?
Users communicate with the kernel with a program
known as shell . A shell is special user program which provide an interface to
user to use operating system services. Shell accept human readable commands
from user and convert them into something which kernel can understand. 
An Operating is made of many components, but its
two prime components are,
Kernel
Shell
A Kernel is at the nucleus of a computer. It makes
the communication between the hardware and software possible. While the Kernel
is the innermost part of an operating system, a shell is the outermost one.
Shell types:
There are two main shells in Linux:
1. The Bourne Shell:
The prompt for this shell is $ and its derivatives are listed below:
POSIX shell also is known as sh
Korn Shell also known as sh
Bourne Again SHell also knew as bash
(most popular)
2. The C shell:
The prompt for this shell is %, and its subcategories are:
C shell also is known as csh
Tops C shell also is known as tcsh
Basic shell
commands:
1).File display
commands:
cat: It is generally used to concatenate the files.
It gives the output on the standard output.
more: It is a filter for paging through text one
screenful at a time.
less: It is used to viewing the files instead of
opening the file. Similar to more command but it allows backward as
well as forward movement.
head : Used to print the first N lines of a
file. It accepts N as input and the default value of N is 10.
tail : Used to print the last N-1 lines of a
file. It accepts N as input and the default value of N is 10.
 
2).file /directory
manipulation commands:
mkdir : Used to
create a directory if not already exist. It accepts the directory name as an
input parameter.
cp : This
command will copy the files and directories from the source path to the
destination path. It can copy a file/directory with the new name to the
destination path. It accepts the source file/directory and destination
file/directory.
mv : Used to move the
files or directories. This command’s working is almost similar
to cp command but it deletes a copy of the file or directory from the
source path.
rm : Used to
remove files or directories.
touch : Used to create
or update a file.
3). Extract,
sort, and filter data Commands: 
grep : This
command is used to search for the specified text in a file.
grep with Regular Expressions:
Used to search for text using specific regular
expressions in file.
sort : This
command is used to sort the contents of files.
wc : Used to
count the number of characters, words in a file.
 cut : Used to
cut a specified part of a file.
 
4). Basic Terminal
Navigation Commands: 
ls : To get the list
of all the files or folders.
ls -l: Optional
flags are added to ls to modify default behavior, listing contents in
extended form -l is used for “long” output
ls -a: Lists
of all files including the hidden files, add -a  flag 
cd : Used to
change the directory.
du : Show disk
usage.
pwd : Show the
present working directory.
man : Used to
show the manual of any command present in Linux.
rmdir : It is used
to delete a directory if it is empty.
locate: It is used
to locate a file in Linux System
echo: This command
helps us move some data, usually text into a file.
df: It is used
to see the available disk space in each of the partitions in your system.
tar : Used
to work with tarballs (or files compressed in a tarball archive)
5). File
Permissions Commands:
 The chmod and chown commands
are used to control access to files in UNIX and Linux systems.
chown: Used to change
the owner of the file.
chgrp : Used to
change the group owner of the file.
chmod : Used to modify
the access/permission of a user.
What Is shell script?
Shell Scripting is an open-source computer
program designed to be run by the Unix/Linux shell. Shell Scripting is a
program to write a series of commands for the shell to execute. It can combine
lengthy and repetitive sequences of commands into a single and simple script
that can be stored and executed anytime which, reduces programming efforts.
 
Why do we Need
Shell Script:
There are many reasons to write shell scripts –
- To
     avoid repetitive work and automation
- System
     admins use shell scripting for routine backups
- System
     monitoring
- Adding
     new functionality to the shell etc.
 
How to write a
Shell script:
Shell Scripts are written using text editors.
On your Linux system, open a text editor program, open a new file to begin
typing a shell script or shell programming, then give the shell permission to
execute your shell script and put your script at the location from where the
shell can find it.
Let us understand the steps in creating a Shell
Script:
- Create
     a file using a vi editor(or any other editor). Name
     script file with extension .sh
- Start the
     script with #! /bin/sh
- Write
     some code.
- Save
     the script file as filename.sh
- For executing the
     script type bash filename.sh
“#!” is an operator called shebang which directs
the script to the interpreter location. So, if we use”#! /bin/sh” the script
gets directed to the bourne-shell.
Eg.
           
#! /bin/sh
             Pwd
             
ls 
 
Shell variables:
Variables store data in the form of characters and
numbers. Similarly, Shell variables are used to store information and they can
used by the shell only.
           
variable ="Hello"
           
 echo $variable
The name of a variable can contain only letters (a
to z or A to Z), numbers ( 0 to 9) or the underscore character ( _).
Shell provides a way to mark variables as read-only
by using the read-only command. After a variable is marked read-only, its value
cannot be changed
Name=”sugumar”
readonly Name
Unsetting or deleting a variable directs the shell
to remove the variable from the list of variables that it tracks. Once you
unset a variable, you cannot access the stored value in the variable.
unset Name
 
Types of shell
variable:
Environment variables –
Variables that are exported to all processes spawned by the shell. Their
settings can be seen with the env command.
Shell (local) variables –
Variables that affect only the current shell.
Arrays in Shell Scrpting:
Shell supports a different type of variable called
an array variable. This can hold multiple values at the same time. Arrays
provide a method of grouping a set of variables. Instead of creating a new name
for each variable that is required, you can use a single array variable that
stores all the other variables.
Following is the method of creating the array,first
you need to define the array name then index and the value.
    NAME[0]="Zara"
    NAME[1]="Qadir"
    NAME[2]="Mahnaz
 
    echo “ ${NAME[*]}"     
output=Zara,Qadir ,Mahnaz
    echo  echo "
${NAME[0]}"   output=Zara
 
Operators in Shell Scripting:
There are 5 basic operators in bash/shell
scripting:
- Arithmetic
     Operators
- Relational
     Operators
- Boolean
     Operators
- Bitwise
     Operators
- File
     Test Operators
 
Arithmetic
Operators:
These operators are used to perform normal
arithmetics/mathematical operations. There are seven arithmetic operators:
- Addition
     (+):
     Binary operation used to add two operands.
- Subtraction
     (-):
     Binary operation used to subtract two operands.
- Multiplication
     (*):
     Binary operation used to multiply two operands.
- Division
     (/):
     Binary operation used to divide two operands.
- Modulus
     (%): Binary
     operation used to find remainder of two operands.
- Increment
     Operator (++):
     Unary operator used to increase the value of operand by one.
- Decrement
     Operator (–):
     Unary operator used to decrease the value of a operand by one.
Relational
Operators:
 Relational operators are those operators
which define the relation between two operands. They give either true or
false depending upon the relation. There are six types:
- ‘==’
     Operator:
     Double equal to operator compares the two operands. It returns true
     if they are equal otherwise returns false.
- ‘!=’
     Operator: Not
     equal to operator returns true if the two operands are not equal otherwise
     it returns false.
- ‘<'
     Operator:
     Less than operator returns true if first operand is less than second
     operand otherwise returns false.
- ‘<='
     Operator:
     Less than or equal to operator returns true if first operand is less than
     or equal to second operand otherwise returns false
- ‘>’
     Operator: Greater
     than operator returns true if the first operand is greater than the second
     operand otherwise returns false.
- ‘>=’
     Operator:
     Greater than or equal to operator returns true if first operand is greater
     than or equal to second operand otherwise returns false.
- Logical
     Operators: They
     are also known as boolean operators. These are used to perform logical
     operations. There are three types:
- Logical
     AND (&&): This is a binary operator, which returns
     true if both the operands are true otherwise returns false.
- Logical
     OR (||):
     This is a binary operator, which returns true if either of the
     operands is true or both the operands are true and returns false if none
     of them is false.
- Not
     Equal to (!):
     This is a unary operator which returns true if the operand is false and
     returns false if the operand is true.
 
Bitwise Operators:
A bitwise operator is an operator used to
perform bitwise operations on bit patterns. There are six types.
- Bitwise
     And (&): Bitwise
     & operator performs binary AND operation bit by bit on the operands.
- Bitwise
     OR (|):
     Bitwise | operator performs binary OR operation bit by bit on the
     operands.
- Bitwise
     XOR (^):
     Bitwise ^ operator performs binary XOR operation bit by bit on the
     operands.
- Bitwise
     compliment (~):
     Bitwise ~ operator performs binary NOT operation bit by bit on the
     operand.
- Left
     Shift (<<): This operator shifts the bits of the
     left operand to left by number of times specified by right operand.
- Right
     Shift (>>):
     This operator shifts the bits of the left operand to right by number of
     times specified by right operand.
 
File Test Operator: 
These operators are used to test a particular
property of a file.
- -d
     operator: This
     operator checks if the given directory exists or not. If it
     exists then operators returns true otherwise returns false
- -e
     operator:
     This operator checks whether the given file exists or not. If it exists
     this operator returns true otherwise returns false.
- -r
     operator:
     This operator checks whether the given file has read access or not. If it
     has read access then it returns true otherwise returns false.
- -w
     operator: This
     operator checks whether the given file has write access or not. If it has
     write then it returns true otherwise returns false.
- -x
     operator: This
     operator checks whether the given file has execute access or not. If it
     has execute access then it returns true otherwise returns false.
- -s operator: This operator checks the size of the given file. If the size of given file is greater than 0 then it returns true otherwise it returns false.
Conditional
statements:
While writing a shell script, there may be a
situation when you need to adopt one path out of the given two paths. So you
need to make use of conditional statements that allow your program to make
correct decisions and perform the right actions.
There are total 5 conditional statements which can
be used in bash programming
- if
     statement
- if-else
     statement
- if..elif..else..fi
     statement (Else If ladder)
- if..then..else..if..then..fi..fi..(Nested
     if)
- switch
     statement
example:
a=20
b=20
  
if [ $a == $b ]
then
    #If they are equal then
print this
    echo "a is equal to
b"
else
    #else print this
    echo "a is not equal
to b"
fi
 
Loops in Shell
Scripting:
 A loop is a powerful programming tool that
enables you to execute a set of commands repeatedly
- while
     statement
- for
     statement
- until
     statement
while
statement 
Here command is evaluated and based on the result
loop will executed, if command raise to false then loop will be
terminated 
syntax:   while condition
              
do statement
for
statement 
Syntax: 
             
 for variable in list/array
              
Do
                  
statement
until
statement 
The until loop is executed as many as times the
condition/command evaluates to false. The loop terminates when the
condition/command becomes true
                
do
 
Functions in Shell
Scripting:
 Functions enable you to break down the
overall functionality of a script into smaller, logical subsections, which can
then be called upon to perform their individual tasks when needed.
Using functions to perform repetitive tasks is an
excellent way to create code reuse. This is an important part of modern
object-oriented programming principles.
they are also same like other programming
languages.by calling the function name you can execute the function and get the
output.
Syntax:  
         
function_name () {
                                           
list of commands
                                           
}
Advantages of Shell Scripting:
1. the command syntax are exactly same as those directly entered command line, so the programmer does not need to switch to entirely different syntax.
2.Writting shell scripts are much quicker.
3.quick start .
4.Interactive debugging
 
 

 
 
Comments
Post a Comment