Linux Shell Scripting

Selection_016SHELL 

Shell is a command language interpreter that executes commands read from the standard input device (keyboard) or from a file. Shell is not part of system kernel, but uses the system kernel to execute programs, create files etc.

So we can say “Shell” is a user program or it’s environment provided for user interaction. 

There are many type shell available with Linux  UNIX-Like System

  • BASH ( Bourne-Again SHell )– Developed by “Brian Fox and Chet Ramey “
  • C SH ( C SHell)– Developed by “Bill Joy
  • K SH (Korn SHeel)– Developed By “ David Korn
  • TCSH

Note: For Knowing all available shell in your system type the command ” #  Cat /etc/shells”  And  that each shell does the same job, but each understand a different command syntax and provides different built-in functions.

Also you can check, what is your current shell where you are working just type the command   #echo $SHELL


Linux Shell Scripting 

In simple way, A Shell Scripts is a text file that contains one or more no. of commands for reboot a task, it helps to  System Administrator or DevOps to make his task easier and automated. And the file extension of any Shell scripts always should be ” <name>.sh ” and execute (x) Permission. Shell is a interface between user and the kernel. Even though there can be  only one kernel a system can have many shell running simultaneously . Whenever  a user enters a command  through keyboard the shell communicates with the kernel  to execute it and then display the output to the user.

Lets have a example:

  • Create a script file  and write your script_code in file and save it.
  • Now make it executable with Excute Permission.
$ sudo vim guarav.sh

#! /bin/bash
~
~ (Your code/command that what do you want to do with 
    this script)
~
:wq

$ sudo chmod +x gaurav.sh

Here as we can see the first line of script’s file is started with #! /bin/bash  it means that  #! – It indicates that the file is an executable shell scripts. its  Know as Command Interpreter.

Some Advantages of BASH Shell 

  • Shell Script can take an input from user, file and output them on screen.
  • Use full to create our own commands and easy to schedule with cron utils.
  • To automate some task day today life.
  • System Administrator’s tasks can also be easy.

Loops in Bash Shell Scripting:

“A group of instruction that is executed repeatedly is called a loop.” Loops are very useful for Bash Shell scripting, it allows us to take a series of commands and keep running them until a particular situation are reached or condition is matched. Loops are useful for automating repetitive tasks. There are different type of loop

  • While
  • for

So, here below some points are important in loops

  • The variable must be initialized which is used in loop, then execution of loop begins.
  • A condition (test) is made at the beginning of each iteration.
  • The body of loop ends with a statement that modifies the value of condition (test) variable.

Lets have an example  of  “for loop” to make a script of ‘Table’s 2

$ vim test.sh

#!/bin/bash
# BASH Shell script of table '2'
echo "Table of 2 "
for i in 1 2 3 4 5 6 7 8 9 10
do
echo "Gaurav $((i+i)) "
done
~ 
:wq

$ chmod +x test.sh
$ ./test.sh

Table of 2 
Gaurav 2 
Gaurav 4 
Gaurav 6 
Gaurav 8 
Gaurav 10 
Gaurav 12 
Gaurav 14 
Gaurav 16 
Gaurav 18 
Gaurav 20 

$_


Advertisements

3 thoughts on “Linux Shell Scripting

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s