540 likes | 810 Views
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.
E N D
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 Command line Interpretation Program Initiation Input-output Redirection Pipeline Connection Substitution of filenames Maintenance of Variables Environment Control Shell Programming
Types of Shell Bourne Shell ,developed by Stephen Bourne Korn Shell ,developed by David Korn C Shell , developed by Bill Joy
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 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 $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 #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 Arithmetic Operator: +,-,\*,/ and % Relation Operator: -lt, -le, -gt, -ge, eq, -ne Logical Operator: -a( AND), -o (OR), !(NOT)
Taking Decision • 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 [ <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 <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 <condition> then statement 1 elif <condition> statement 2 else statement 3 fi
case-esac statement #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 • Using a while statement • Using a until statement • Using a for statement
while statement count=1 while[ $count –le 10 ] do echo $count count=`expr $count + 1` done while [<condition>] do statement1 statement2 done
count=1 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 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 • 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’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 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 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] 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 • Assignment Operator :- = • Arithmetic Operators :- + , - , * , / , % • Logical Operator :- || , && , ! • Comparison Operators :- < ,> ,<= ,>= ,== ,!= • String Comparison:- =~ , !~
Examples $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 $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 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 /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 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 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 $awk ‘/sales/ { print }’ file1> Nfile1 $cat Nfile1
BEGIN and END patterns 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 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 $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
ARRAY • 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.