Showing posts with label shell scripting tutorial. Show all posts
Showing posts with label shell scripting tutorial. Show all posts

Command line argument in shell script

Passing arguments to the main program when it start is know as command line arguments   

In shell scripting arguments can be described like below

$0 - shows program name which you running
$1 - shows first argument 
$2 - display second argument
$3 - display third argument
$n - n th argument
$# - shows number of arguments passed to the program
$* - shows entire string of parameters you passed


Break and Continue statement in shell script

DESCRIPTION:
All statement inside the loop executed as long as some condition are true.

If break placed inside the loop, when loop reach the break statement it will terminated out from the loop.

If continue placed inside the loop, when loop reach the continue statement it will not execute next lines of the loop and it will go to the next iteration.

Function in shell script

DESCRIPTION: 
Function is the piece of code, which executed when we call.
When thinking about the function in any computer language, three points comes in mind

  1. Writing function code and calling that function when we need
  2. Passing arguments to the function
  3. Returning value from function

Next i going to elaborate about the 3 points i mentioned above 

Until loop in shell script

Until loop is opposite to while loop.

It will execute the loop as long as condition false.


When condition meet true it will terminate from the loop.

While loop in shell script

While will execute the loop as long as condition true.

When condition meet false it will terminate from the loop.

For loop in shell script

For loop will execute the loop again and again, until particular condition is true.

Case statement in shell script

Case is used to select one value from multiple option.

It is alternate and easy way to multi level if statement.

Case statement end with 'esac'.

If loop in shell script

if script:
#!/bin/bash
echo -n "enter the key : "
read key
if [ $key -eq 1122 ]
then
   echo "you entered correct key"
fi
output:
enter the key : 1122
you entered correct key


Numeric comparision in shell script

eq - equal:
     var1 -eq var2
     var1 equal to var2

ne - not equal:
     var1 -ne var2
     var1 not equal to var2

gt - greater :
    var1 -gt var2
    var1 greater than var2

Variables in shell script

Variables are use to assign the value of data types such as integer, float, string, etc.   

In shell scripting no need to specify types for your variable.
Create integer variable
     In C language
           int a = 10;
     In shell script
           a=10

First Shell Script In linux



1. Open terminal window in Linux

2. Create a directory for all of you shell script you going to do
              mkdir shell

3. Go to the directory and create a file to  write your script
             cd shell
             vi first.sh

Introduction to shell script

KERNEL:

Kernel act as the mediator between the computer hardware and application.

SHELL:

Shell reads the commands from the user and translate them to understandable by kernel.

SHELL SCRIPT:

Shell script is a file which stores the series of commands. Shell will execute this text file instead of entering the commands.