0% found this document useful (0 votes)
81 views49 pages

Unit Part II

This document provides an introduction to shell scripting and AWK programming. It discusses the functions of the shell including command line interpretation, program initialization, input-output redirection, and maintenance of variables. It describes different types of shells like Bourne shell, Korn shell, and C shell. It covers escape sequences, examples of using echo and tput commands, command grouping, positional parameters, shift command, string and file operators, conditional statements like if-then-fi and case-esac, and looping statements like while and until.

Uploaded by

Shashuu Smash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views49 pages

Unit Part II

This document provides an introduction to shell scripting and AWK programming. It discusses the functions of the shell including command line interpretation, program initialization, input-output redirection, and maintenance of variables. It describes different types of shells like Bourne shell, Korn shell, and C shell. It covers escape sequences, examples of using echo and tput commands, command grouping, positional parameters, shift command, string and file operators, conditional statements like if-then-fi and case-esac, and looping statements like while and until.

Uploaded by

Shashuu Smash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

INTRODUCTION TO SHELL SCRIPT

&
AWK PROGRAMMING

1
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.

2
Functions of Shell

1. Command Line Interpretation

2. Program Initialization

3. Input-Output Redirection

4. Pipeline Connection

5. Substitution of Filename

6. Maintenance of variable

7.Environment Variable

8. Shell programming
3
Types of Shell

Bourne Shell

• Developed by Stephen Bourne

Korn Shell

• Developed by David Korn

C Shell

• Developed by Bill Joy

4
Escape Sequence

Option Action
\b Back Space
\n New line
\r Carriage Return
\c Positioning the Cursor
\t Tab
\a Alert (Beep Sound)
\\ Back Slash
\‟ Single Quote
\‟‟ Double Quote

5
Example

$echo “There is always one more\b\b\b\b\b \t – By law”


$echo “I like work..\r I can sit and watch it for hours.”
O/P : I can sit and watch it for hours.

$echo “Enter your choice…..”


O/P : Enter your choice…..
(Cursor is blinking here)
$echo “Enter your choice…….\c”
O/P : Enter your choice…..(Cursor is blinking here)

6
Escape Sequence

Option Action

\033[0m Normal Character

\033[1m Bold Character

\033[4m Underlined Character

\033[5m Blinking Character

\033[7m Reverse Video Character

7
Example
$echo “\033[1m This is Bold”

O/P :This is Bold

$echo “033[7m] This is Bold and Reverse”

O/P: This is Bold and Reverse

8
The tput command

 Other than echo there is a command called


tput using which we can control the way the
output is displayed on the screen.

 Since Unix permits usage of different


terminal types it maintains a database which
contains useful information about these
terminal types

9
Option Action
clear Clear the screen
cup r c Move cursor to row r and column c
bold Bold display
blink Blinking display
rev Reverse video display
smso Start mode bold
rmso End mode bold
smul Start mode underline
rmul End mode underline
bel echo the terminal‟s “bell” character
lines echo number of lines on the screen
cols echo number of columns on the screen
ed Clear to end of the display
el Clear to end of line

10
#Demonstration of tput command
tput bell
tput clear
echo “ Total no. of column on screen =\c”
tput cols
echo “Total no. of row on screen = \c”
tput lines
echo “This is a normal message”
tput blink
echo “This is a blinking message”
tput bold
echo “This is a bold message”
tput cup 10 20 # Positions cursor at 10th row and 20th column
echo “Testing of tput”
tput smso # start bold mode
Echo “ The bold has begun………..”
Tput rmso # end bold mode

11
Command Grouping
Syntax:-

command1;command2;command3

For Example :-
1. date;pwd;ls

II. (date;pwd;ls)>result

For Output:- cat result

12
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

13
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.

14
$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
15
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

16
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
17
$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 $*

18
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 $*

19
Operators
Arithmetic Operator:
+,-,\*,/ and %
Relation Operator:
-lt, -le, -gt, -ge, eq, -ne
Logical Operator:
-a( AND), -o (OR), !(NOT)

20
String Operators

Str1=Str2 True if Str1 is the same length and


contains the same characters as Str2
Str1 !=Str2 True if Str1 is not the same as Str2
-n Str1 True if the length of Str1 is greater
than o( is not null)
-z Str1 True if Str1 is null
Str1 True if Str1 in not null

21
Testing for files

-r filename True if the user has read permission

-w filename True if the user has write permission


-x filename True if the user has execute permission

-f filename True if filename is a regular file


-d filename True if filename is a directory
-c filename True if filename is a character special file
-b filename True if filename is a block special file
-s filename True if the size of filename is not zero

22
Taking Decision
 The if-then-fi statement
 The if-then-else-fi statement
 The if-then-elif-else-if statement
 The case-esac statement

23
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

24
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

25
if–then–elif–else-fi statement

if <condition>
then
statement 1
elif <condition>
statement 2
else
statement 3
fi

26
case-esac statement
case <value> in #Example of case statement
choice1)
do this echo “Enter the number from 1 to 3 : “
and this
read num
;;
choice2)
do this case $num in
and this 1) echo You Entered 1
;; ;;
*) 2) echo You Entered 2
;;
do this
3) echo You Entered 3
and this ;;
;; *) echo I said 1 to 3
esac ;;
esac

27
Looping Statement
 Using a while statement
 Using a until statement
 Using a for statement

28
while statement
while [<condition>] count=1
do while[ $count –le 10 ]
statement1
do
statement2
done echo $count
count=`expr $count + 1`
done

29
count=1
until statement
while[ $count –lt 10 ]
do
until [<condition>] echo $count
do count=`expr $count + 1`

statement1 done
statement2
count=1
done
until[ $count –gt 10 ]
do
echo $count
count=`expr $count + 1`

done 30
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

31
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.

32
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

33
awk‟ rules of the thumb or Simple awk filter

Syntax:-
$awk [option] „selection criteria { print }‟
<filename>
Eg:-
1. $awk „/sales/‟ file1
2. $awk „/sales/ { print }‟ file1
3. $awk „/sales/ { print $0}‟ file1

34
Column Selection or Splitting line into fields

Eg:-
1. $awk „/sales/ { print $1 $2 }‟ file1
2. $awk „/sales/ { print $1,$2 }‟ file1
3. $awk „/sales/ { print $2,$1 }‟ file1
4. $awk „/sales/ { print $3,$1,$2 }‟ file1

35
Extended Regular Expression [ERE]

Eg:-
1. $awk „/[a-m]/ { print }‟ file1
2. $awk „/[a-m].*/ { print $1,$2 }‟ file1
3. $awk „/M[CB]A/ { print $0 }‟ file1
4. $awk „/M[!CB]A/ { print $3,$1,$2 }‟ file1

36
Numerical Operations

1. Assignment Operator :- =
2. Arithmetic Operators :- + , - , * , / , %
3. Logical Operator :- || , && , !
4. Comparison Operators :- < ,> ,<= ,>=
,== ,!=
5. String Comparison:- =~ , !~

37
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

38
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

39
File2
Files 101 Ram sales good
102 Shyam Sales Good
$cat>rep.awk 103 John Purchase Good
/sales/ { print $1,$3} 104 Hari Marketing Good

^d
$cat rep
O/P:- /sales/ { print $1,$2,$3}

$awk –f rep.awk file2


101 Ram sales
102 Shyam Sales

40
$cat>rep.awk
/sales/ {
name = $2
dept = $3
print “Myself” name “and department is” dept
}
^d File2
$awk –f rep.awk file2 101 Ram sales good
102 Shyam Sales Good
O/P:- 103 John Purchase Good
Myself Ram and department is sales
104 Hari Marketing Good
Myself Shyam and department is sales

41
User Defined Variables
Sundry
Pen 110
$cat>Hike.awk
Pencil 100
{
Marker 200
name = $1
Duster 150
price = $2
inc = 10
new_price = price + inc
print name , price , newprice
}^d Pen 110 120
$awk –f Hike.awk Sundry Pencil 100 110
Marker 200 210
Duster 150 160
42
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

43
Redirection
$awk „/sales/ { print }‟ file1> Nfile1

$cat Nfile1

44
BEGIN and END patterns

The BEGIN subsection can contain all the steps


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

The END subsection takes care of any statement


END to be appended or totals to be printed etc.

45
BEGIN {
print “THIS IS TRIAL”
print “NAME OF OPRICE NPRICE”
}
/pen/ {
name = $1
price = $2
oprice = price
nprice = oprice * 1.25 Sundry
} Pen 110
END { Pencil 100
print name oprice nprice
Marker 200
}
Duster 150

$awk –f Bend.awk Sundry

Pen 110 137.5

46
Built in variables
VARIABLE FUNCTION
Cumulative number of lines read
NR
FS Input field separator

OFS Output field separator

NF Number of fields in current line

FILENAME Current input file

ARGC Number of arguments in command


line
ARGV List of arguments

$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 47
Built in Functions
Function Description
int(x) Returns integer value of x

sqrt(x) Returns square root of x

length Returns length of complete line

length(x) Returns length of x

substr(stg,m,n) Returns portion of string of length n,


starting from position m in string stg
Index(s1,s2) Retruns position of string s2 in string s1

Split(stg,arr,ch) Splits string stg into array arr using ch as


delimeter
system(“cmd”) Runs UNIX command cmd and returns its
exit status

48
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.

49

You might also like