Introduction to shell script awk programming
Download
1 / 44

INTRODUCTION TO SHELL SCRIPT & AWK PROGRAMMING - PowerPoint PPT Presentation


  • 64 Views
  • Uploaded on

INTRODUCTION TO SHELL SCRIPT & AWK PROGRAMMING. What is Shell. It is the command interpreter which interprets the user commands and convey them to the kernel that executes them. Thus is acts as a mediator between the user and kernel. The Functions of Shell.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' INTRODUCTION TO SHELL SCRIPT & AWK PROGRAMMING' - toyah


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

What is shell
What is Shell

It is the command interpreter which interprets the user commands

and convey them to the kernel that executes them. Thus is acts as

a mediator between the user and kernel.


The functions of shell
The Functions of Shell

Command line Interpretation

Program Initiation

Input-output Redirection

Pipeline Connection

Substitution of filenames

Maintenance of Variables

Environment Control

Shell Programming


Types of shell
Types of Shell

Bourne Shell ,developed by Stephen Bourne

Korn Shell ,developed by David Korn

C Shell , developed by Bill Joy


Escape sequence
Escape Sequence

\b Back Space

\n New Line

\r Carriage Return

\t Tab

\a Alert (Beep Sound)

\\ Back Slash

\’ Single Quote

\” Double Quote


Command grouping
Command Grouping

Syntax:-

command1;command2;command3

For Example :-

1. date;pwd;ls

II. (date;pwd;ls)>result

For Output:- cat result


set

$set friends come and go but enemies accumulate

$echo $1 $2 $3 $4 $5 $6 $7

O/P:- friends come and go but enemies accumulate

$echo $1 $2 $3 $4

O/P:- friends come and go


Positional parameters
Positional Parameters

$0 – Refers to the name of the command.

$1 – Refers to the first argument.

$2 – Refers to the second argument. … & so on

$* - Refers all the argument.

$# - Refers the total no. of arguments.

$? - Returns the exit status of last executed command.

$! - Returns the Process Identification number (PID) of last background command (command ended with & )

$$ - Returns the PID of the current shell.


$set Do you want credit or results

$set A smiling face is always beautiful

$echo $1 $2 $3 $4 $5 $6

O/P:- A smiling face is always beautiful

$cat lucky

O/P:- Give luck a little time

$set `cat lucky`

$set $1 $2 $3 $4 $5

O/P:-Give luck a little time


$set Do you want credit or results

$set A smiling face is always beautiful

$echo $1 $2 $3 $4 $5 $6

O/P:- A smiling face is always beautiful

$cat lucky

O/P:- Give luck a little time

$set `cat lucky`

$set $1 $2 $3 $4 $5

O/P:-Give luck a little time


Using Shift on Positional Parameters

“Set is used to set upto 9 words”

$set You have the capacity to learn from mistakes.

You will learn a lot in your life.

$echo $1 $2 $3 $4 $5 $6 $7 $8 $9 $10 $11

O/P:-

You have the capacity to learn from mistakes. You

You0 You1


shift

$shift 7

$echo $1 $2 $3 $4 $5 $6 $7 $8 $9

mistakes. You will learn a lot in your life.

$set You have the capacity to learn from mistakes.

You will learn a lot in your life.

$a=$1

$b=$2

$c=$3

$d=$4

$e=$5 $f=$6 $g=$7


$shift 7

$echo $a $b $c $d $e $f $g $1 $2 $3 $4 $5 $6 $7 $8 $9

O/P:- You have the capacity to learn from mistakes. You will learn a lot in your life.

OR

$echo $*


Write a shell program to perform the positional parameters and shift command
Write a shell program to perform the positional parameters and shift command

#Program of positional parameters and shift command

set If you are headed in the wrong direction, God allows U turns.

echo $*

shift 1

echo $*

shift 1

echo $*

shift 1

echo $*

Shift

echo $*


Operators
Operators and shift command

Arithmetic Operator:

+,-,\*,/ and %

Relation Operator:

-lt, -le, -gt, -ge, eq, -ne

Logical Operator:

-a( AND), -o (OR), !(NOT)


String Operators and shift command


Testing for files and shift command


Taking decision
Taking Decision and shift command

  • The if-then-fi statement

  • The if-then-else-fi statement

  • The if-then-elif-else-if statement

  • The case-esac statement


If then fi statement
if–then–fi statement and shift command

if [ <condition> ]

then

statement 1….statement n

fi

eg- echo “Enter source and destination file names

read source destination

if [ cp $source $destination ]

then

echo “ File Copied Successfully”

fi


If then else fi statement
if–then–else-fi statement and shift command

if <condition>

then

statement 1

else

statement 2

fi

eg- echo “Enter source and destination file names

read source destination

if [ cp $source $destination ]

then

echo “ File Copied Successfully”

else

echo “Failed to copy the file

fi


If then elif else fi statement
if–then–elif–else-fi statement and shift command

if <condition>

then

statement 1

elif <condition>

statement 2

else

statement 3

fi


Case esac statement
case-esac statement and shift command

#Example of case statement

echo “Enter the number from 1 to 3 : “

read num

case $num in

1) echo You Entered 1

;;

2) echo You Entered 2

;;

3) echo You Entered 3

;;

*) echo I said 1 to 3 ;;

esac

case <value> in

choice1)

do this

and this

;;

choice2)

do this

and this

;;

*)

do this

and this

;;

esac


Looping statement
Looping Statement and shift command

  • Using a while statement

  • Using a until statement

  • Using a for statement


While statement
while statement and shift command

count=1

while[ $count –le 10 ]

do

echo $count

count=`expr $count + 1`

done

while [<condition>]

do

statement1

statement2

done


Until statement

count=1 and shift command

while[ $count –lt 10 ]

do

echo $count

count=`expr $count + 1`

done

until statement

until [<condition>]

do

statement1

statement2

done

count=1

until[ $count –gt 10 ]

do

echo $count

count=`expr $count + 1`

done


For statement
for statement and shift command

for control_variable in value1 value2…. do

statement1

statement2

done

for word in High on a hill was a lovely mountain

do

echo $word

done


Awk programming
AWK Programming and shift command

  • The command grep lets you locate words or patterns in a file. Next sed lets you add or delete lines depending upon a pattern. You could replace the words or change the case and so forth. Now we come to the granddaddy of utilities i.e. awk.

  • awk stands for Aho , Weinberger & Kernighan the creators of this utility.

  • awk can do pattern matching, print selected records/fields and comparison & computation.

  • We will take a look at how awk operates. awk operates like sed i.e. It processes the input line by line. Also it has to have a context or reference. The similarity ends here. When it comes to actions to be performed on the selected lines awk provides you a lot more features.


Awk operations
awk operations and shift command

  • awk’s rules of the thumb or Simple awk Filters

  • Column selection or Splitting line into fields

  • Extended Regular expression [ERE]

  • Numerical Operators

  • The –F & f options

  • Files

  • User Defined Variables

  • Formatting Output

  • Redirection

  • BEGING and END Patterns


Awk rules of the thumb or simple awk filter
awk’ rules of the thumb or Simple awk filter and shift command

Syntax:-

$awk [option] ‘selection criteria { print }’ <filename>

Eg:-

  • $awk ‘/sales/’ file1

  • $awk ‘/sales/ { print }’ file1

  • $awk ‘/sales/ { print $0}’ file1


Column selection or splitting line into fields
Column Selection or Splitting line into fields and shift command

Eg:-

  • $awk ‘/sales/ { print $1 $2 }’ file1

  • $awk ‘/sales/ { print $1,$2 }’ file1

  • $awk ‘/sales/ { print $2,$1 }’ file1

  • $awk ‘/sales/ { print $3,$1,$2 }’ file1


Extended regular expression ere
Extended Regular Expression [ERE] and shift command

Eg:-

  • $awk ‘/[a-m]/ { print }’ file1

  • $awk ‘/[a-m].*/ { print $1,$2 }’ file1

  • $awk ‘/M[CB]A/ { print $0 }’ file1

  • $awk ‘/M[!CB]A/ { print $3,$1,$2 }’ file1


Numerical operations
Numerical Operations and shift command

  • Assignment Operator :- =

  • Arithmetic Operators :- + , - , * , / , %

  • Logical Operator :- || , && , !

  • Comparison Operators :- < ,> ,<= ,>= ,== ,!=

  • String Comparison:- =~ , !~


Examples
Examples and shift command

$awk ‘{ print $1,$2,$3+10,$4}’ file1

$awk ‘{ print $1-5,$2,$3,$4*4}’ file1

$awk ‘$4 >3’ file1

$awk ‘$4 <3’ file1

$awk ‘$4 <=3’ file1

$awk ‘$4 !=3’ file1

$awk ‘$4 ==3’ file1


The f option
The –F option and shift command

$awk –F”|” ‘/sales/{ print $1,$2}’ file1

$awk –F”|” ‘/sales/{ print $1,$2,$3}’ file1

$awk –F”|” ‘/sales/{ print $0}’ file1

$awk –F”|” ‘/sales/{ print }’ file1

$awk –F”|” ‘/sales/’ file1

The –f option :- …PTO


Files
Files and shift command

File2

101 Ram sales good

102 Shyam Sales Good

103 John Purchase Good

104 Hari Marketing Good

$cat>rep.awk

/sales/ { print $1,$3}

^d

$cat rep

O/P:- /sales/ { print $1,$2,$3}

$awk –f rep.awk file2

101 Ram sales

102 Shyam Sales


$cat>rep.awk and shift command

/sales/ {

name = $2

dept = $3

print “Myself” name “and department is” dept

}

^d

$awk –f rep.awk file2

O/P:-

Myself Ram and department is sales

Myself Shyam and department is sales

File2

101 Ram sales good

102 Shyam Sales Good

103 John Purchase Good

104 Hari Marketing Good


User defined variables
User Defined Variables and shift command

Sundry

Pen 110

Pencil 100

Marker 200

Duster 150

$cat>Hike.awk

{

name = $1

price = $2

inc = 10

new_price = price + inc

print name , price , newprice

}^d

$awk –f Hike.awk Sundry

Pen 110 120

Pencil 100 110

Marker 200 210

Duster 150 160


Formatting output
Formatting Output and shift command

File Name :- Extra.awk

/pen/ {

name = $1

price = $2

nprice = price + price * 0.325

printf “%20s %10s \n”, name,nprice

}

$awk –f Extra.awk Sundry


Redirection
Redirection and shift command

$awk ‘/sales/ { print }’ file1> Nfile1

$cat Nfile1


Begin and end patterns
BEGIN and END patterns and shift command

The BEGIN subsection can contain all the steps that have to be worked out before awk begins to accept and work on the input file. This section can be use to print heading or any comment lines.

BEGIN

The END subsection takes care of any statement to be appended or totals to be printed etc.

END


PROBLEM and shift command

BEGIN {

print “THIS IS TRIAL”

print “NAME OF OPRICE NPRICE”

}

/pen/ {

name = $1

price = $2

oprice = price

nprice = oprice * 1.25

}

END {

print name oprice nprice

}

Sundry

Pen 110

Pencil 100

Marker 200

Duster 150

File named

Bend.awk

$awk –f Bend.awk Sundry

EFFECT

OUTPUT

Pen 110 137.5


Built in variables
Built in variables and shift command

$awk ‘BEGIN { FS = “|”

>NF != 6{ print “Record No “, NR , “has” , NF , “fields” }’ emp.txt

O/P:- Record No 6 has 4 fields

Record No 17 has 5 fields

EXAMPLE


Built in functions
Built in Functions and shift command


Array
ARRAY and shift command

  • Awk handles one-dimensional arrays.

  • The index for an array can be virtually anything; It can even be a string.

  • No array declarations are necessary;

  • An array is considered to be declared the moment it is used and is automatically initialized to zero, unless initialized explicitly.


ad