Shell Programming Ver 4.0 1 Objectives Introduction to Shells Introduction to shell programming ...

96
Shell Programming Ver 4.0 1 Objectives Introduction to Shells Introduction to shell programming Shell variables Arithmetic Operations Conditions - test Conditional and looping statements The getopts command SHELL PROGRAMMING

Transcript of Shell Programming Ver 4.0 1 Objectives Introduction to Shells Introduction to shell programming ...

Page 1: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 1

Objectives Introduction to Shells

Introduction to shell programming

Shell variables

Arithmetic Operations

Conditions - test

Conditional and looping statements

The getopts command

Objectives Introduction to Shells

Introduction to shell programming

Shell variables

Arithmetic Operations

Conditions - test

Conditional and looping statements

The getopts command

SHELL PROGRAMMING

Page 2: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 2

Shell Programming (contd.)

Introduction

Unix offers several command processors

Called as shells

Primary interface to the user

The two basic shells are

Bourne Shell

C Shell

The shells are

Programs under execution

Page 3: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 3

Shell Programming (contd.)

Introduction

The various shells

Name of the Formal name

Shell program

sh Bourne Shell

rsh Restricted Bourne Shell

ksh Korn Shell

rksh Restricted Korn Shell

bash Bourne Again Shell

csh C Shell

Page 4: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 4

Shell Programming (contd.)

Introduction

The Bourne Shell

Named after the developer Stephen R. Broune

The prompt displayed by this shell is “ $ ”

Supports for processes both

Foreground and background

Pipes, filters etc.

Most unix implementations includes this shell

Page 5: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 5

Shell Programming (contd.)

Introduction

C-Shell

Developed by Bill Joy

Advantages

History mechanism

Job control

Aliasing

- Alternate names for commands

Page 6: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 6

Shell Programming (contd.)

Introduction

Restricted Shell

Special version of Bourne Shell

Has a limited set of capabilities and privileges

Korn Shell

Named after David Korn

Up-ward Compatible extension of Bourne Shell

Has the history, job control and command aliasing

Page 7: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 7

Shell Programming (contd.)

Introduction

Restricted Korn Shell

Restricted version of Korn Shell

Bourne Again Shell

Product of Free Software Foundation

Developed by Brian Fox

Extended version of Bourne Shell

Page 8: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 8

Shell Programming (contd.)

Introduction

What is a shell ?

A program to interpret the commands

It reads, interprets and process the command

The shell signals it is ready by displaying a prompt

On a UNIX system there are various shells

The UNIX system starts a default shell for a user

Reading an entry from the /etc /passwd file

Page 9: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 9

Shell Programming (contd.)

Introduction to shell scripts

All shells has a built in language

A user can write a script using the language and

The shell will execute that script

The script does not require compilation or linking

The shell interprets the script and

executes directly, using kernel facility

Shell programs can be used for various tasks

Page 10: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 10

Shell Programming (contd.)

Introduction

Shell Script Uses

Customizing the work environment

Automating some routine tasks

Backing up all the files

Producing the sales report every month

Executing system procedures

Shutting down

formatting the disk etc…

Page 11: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 11

Shell Programming (contd.)

Introduction

The # symbol marks the beginning of a comment

The execute permission for the file must be set

chmod 744 program1

# program1

ls

who

pwd

Page 12: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 12

Shell Programming (contd.)

Introduction

When a shell script is executed

The login shell creates a new shell for the script to be executed

The login-in shell waits for the new shell to terminate

Any task that can be achieved at the shell prompt

Can be achieved in the shell script

Page 13: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 13

Shell Programming (contd.)

Shell variables

Provided to store and manipulate data

Any number of variables can be

Created and Destroyed

Rules for building a variable

A variable name is a combination of

Alphabets, digits and underscore (‘ _ ‘ )

Comas and blanks are not allowed

Page 14: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 14

Shell Programming (contd.)

Shell variables

Rules for building a variable ( Contd. )

The first character must be an alphabet

They can be of any length

Variable names are case-sensitive

#program2

echo What is your name\?

read name

echo Hello $name

Page 15: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 15

Shell Programming (contd.)

Shell variables

The assignment operator

To assign values to a variable

There should be no space on either side of the assignment

operator

The variable is created if it did not exist

name=samage=20dirname=/usr/sam

Page 16: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 16

Shell Programming (contd.)

Shell variables

System variables

The shell provides the values for these variables

Used by the system and govern the environment

These variables can be changed

To customize to system environment

Example

The dollar prompt can be changed

$ PS1=“Next”

Page 17: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 17

Shell Programming (contd.)

System variables

Variable Meaning

PS2 System prompt2 ( default “>” )

PATH Path which shell searches to execute

HOME Default working directory

LOGNAME login name

IFS Internal field separator

SHELL Default working shell

TERM Name of the terminal

TZ Time zone

Page 18: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 18

Shell Programming (contd.)

System variables

The list of all system variables and values can be

displayed using the set command

$ set

HOME=/usr/sam

IFS=

LOGNAME=sam

MAIL=/usr/spool/mail/sam

PATH=/bin:/usr/bin:/usr/sam:.

...

Page 19: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 19

Shell Programming (contd.)

Shell Keywords

Borne shell has a set of predefined words

They cannot be used as variable names

echo read set unset readonly shift export

if else fi while do done for

until case esac break continue exit return

trap wait eval exec ulimit umask

Page 20: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 20

Shell Programming (contd.)

Shell variables

All shell variables are string variables

a=20 is stored as characters 2 and 0

A variable can contain more than one word

The assignment must be made in double quotes

$ c=“Two Words”

$ echo $c

Page 21: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 21

Shell Programming (contd.)

Shell variablesAssignment can be carried out in a single line

All the variables defined in a shell script Are destroyed after the shell script execution is over

The user defined variables can be defined at the Dollar prompt or

In a shell script

$name=sam age=20$echo Name of the boy is $name,and his age is $ageName of the boy is sam,and his age is 20

Page 22: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 22

Shell Programming (contd.)

Shell variables

A null variable is a variable which is not initialized

$ a=“ ” b=‘ ‘ c=

Echoing a null variable a blank line appears on the

screen

The shell ignores a null variable in a command

$ var1=“ ” var2=“ ”

$ echo wc -l $var1 $var2 file1

178

Page 23: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 23

Shell Programming (contd.)

Shell variables

Constants are variables made read-only

$a=20

$readonly a

All readonly variables can be listed

By entering readonly at command prompt

$ readonly

Page 24: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 24

Shell Programming (contd.)

Shell variables

A variable can be removed by using the unset command

$ unset b

The variable and the value are erased from the memory

Unsetting a system variable is not allowed

$ unset PS1 ( error )

Page 25: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 25

Shell Programming (contd.)

Command line argumentsTo convey information from the user to the program

The user specifies arguments at the command line

These arguments can be accessed using Positional parameters

The positional parameters are specified in the script as $1 to $9

Example- To copy a file from a srcfile to dstfile

$ mycopy srcfile dstfile

Page 26: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 26

Shell Programming (contd.)

Command line arguments

The statement cp $1 $2 is translated to cp srcfile dstfile

Write a shell script which accepts a filename Changes the permissions to rwxr--r-- ( 744 )

#mycopy

#usage: mycopy <source file> <destination file >

cp $1 $2

echo copy completed.

cat $2

Page 27: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 27

Shell Programming (contd.)

Positional ParametersThe user cannot assign values to positional parameters

$1=sam $2=100 (error )

The set command can be used to assign values to the positional parameters

$ set This is my first shell script

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

This is my first shell script

$ set sam is good at shell programming

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

sam is good at shell programming

Page 28: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 28

Shell Programming (contd.)

Positional Parameters

The command in the quoting metacharacter is replaced by the output of the command

Theses quote are called reverse quote or accent graves

Write a program to rename a filename to filename.logname

Write a program to display date in the desired format

$ set ‘cat lucky‘

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

Page 29: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 29

Shell Programming (contd.)

Positional ParametersThe parameter $# can be used to find out the number of

parameters from set command parameters from the command line

The Unix metacharacter * represents all the files when used as an argument

$ prgname *

# prgname

#usage prgname <list of files >

echo Total number of files = $#

Page 30: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 30

Shell Programming (contd.)

Positional Parameters If the number of parameters are greater than nine

The shift command can be used to shift the parameters The $* can be used for all the positional parameters

$ set a b c d e f g h i j k l m n o p

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

a b c d e f g h i

$ shift 7

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

h I j k l m n o p

Page 31: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 31

Shell Programming (contd.)

Arithmetic in shell scriptsThe values stored in the variables are of type characterThe expr can be used to evaluate an arithmetic

expression

# example of arithmetic program

a=20 b=10

echo ‘expr $a + $b‘

echo ‘expr $a - $b‘

echo ‘expr $a \* $b‘

echo ‘expr $a / $b‘

echo ‘expr $a % $b‘

Page 32: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 32

Shell Programming (contd.)

Arithmetic in shell scriptsTerms of the expression must be separated by blanksThe priorities for arithmetic operators are

/, * and % first priority

+ and - Second priority

If the operators of the same priority occur in an expression

Preference is give to the one occurring first

$a \* $b + $c / $d $a \* $b is evaluated first

To force a particular order parenthesis must be used$a \* \( $b + $c \) / %d

Page 33: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 33

Shell Programming (contd.)

Arithmetic in shell scriptsThe expr can be used only for integers

For Floating point the command bc is used

# example of floating point arithmetic program

a=15.5 b=7.8

c=‘echo $a + $b | bc‘

d=‘echo $a - $b | bc‘

e=‘echo $a \* $b | bc‘

f=‘echo $a / $b | bc‘

Page 34: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 34

Shell Programming (contd.)

Escape sequences

The echo statement can contain escape sequences

Sequence Behavior\n newline character\r Carriage return\b backspace\t Tab\c cursor position

Page 35: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 35

Shell Programming (contd.)

Read command

If only two values are supplied the third one would be treated as null variable

If more than three values are supplied The first three are supplied to a, b and c The remaining are appended to the third

#prgname

echo Enter the values of a,b and c

read a b c

echo a b c

Page 36: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 36

Shell Programming (contd.)

ExercisesA persons basic salary is entered through a keyboard

His DA is 35%of basic

HRA is 20% of his basic

Write a program to calculate his gross salary

If a five digit number is entered through a keyboard Write a program to calculate the sum of the digits

The file /etc/passwd contains information about all users Write a program that takes a login name as an argument,obtain

information from the /etc/password

Print the information in an understandable format

Page 37: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 37

Shell Programming (contd.)

Control Instructions

Specify the order of execution of the program

Sequence Control Instructions

Selection or Decision Control Instructions

Loop Control Instructions

Case Control Instructions

The Sequence Control Instructions ensures that

The instructions are executed in the same order as they appear

Page 38: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 38

Shell Programming (contd.)

Decision ControlBourne shell offers four decision making instructions

if-then-fi statement if-then-else-fi statement if-then-elif-else-fi statement case-esac statement

The simplest form of if-then-fi statement is

if control commandcommand1

fi

Page 39: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 39

Shell Programming (contd.)

if-then-fi statement

This statement tests the exit status of the command

Weather it was executed successfully or not

The exit status is 0 on success

The exit status is 1 when not successful

Example

If cp command is successful in copying it returns 0

If grep is successful in locating the pattern, it returns 0

The control command can be any valid UNIX command

Page 40: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 40

Shell Programming (contd.)

if-then-fi statement If the exit status of the control command is 0

The command1 is executed else it is not executed

Every if statement must end with fi

echo Enter source and destination files

read src dst

if cp $src $dst

then

echo file Successfully copied

fi

Page 41: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 41

Shell Programming (contd.)

if-then-else-fi statement

echo Enter source and destination files

read src dst

if cp $src $dst

then

echo file Successfully copied

# . . . . .

else

echo Failed to copy

fi

Page 42: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 42

Shell Programming (contd.)

if-then-else-fi statement

The group of commands between then and else is called

if block

The group of commands between else and fi is called

else block

Even if there is only one command to be executed

The fi cannot be dropped

Page 43: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 43

Shell Programming (contd.)

Test commandPerforms a test and returns the result as success or

failure

#Use of test command

echo Enter a number from 1 to 10

read num

if test $num -lt 5

then

echo the number is less than five

fi

Page 44: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 44

Shell Programming (contd.)

Test command

The test command can carry out several tests

Numerical tests

Comparing numbers if they are greater, lesser or equal to

String tests

Testing if strings are same,greater than zero etc

File tests

Tests if it is a directory,character special files etc

Page 45: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 45

Shell Programming (contd.)

Test commandNumerical test

Operator Meaning

-gt greater than

-lt less than

-ge greater than or equal to

-le less than or equal to

-ne not equal to

-eq equal to

Page 46: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 46

Shell Programming (contd.)

Test command# Example of numeric testecho Enter basic salaryread bsif [ $bs -lt 2500 ]then

hra=‘echo $bs \* 10 / 100 | bc‘da=‘echo $bs \* 90 / 100 bc‘

elsehra=500da=‘echo $bs \* 98 / 100 | bc‘

figs=‘echo $bs + $hra + $da | bc‘echo Gross salary = Rs. $gs

Page 47: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 47

Shell Programming (contd.)

Test commandString test

Operator Meaning

string1 = string2 True if the strings are same

string1 != string2 True if the strings are different

-n string True if the length of the string

is greater than zero

-z string True if the length is zero

string True if the string is not null string

Page 48: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 48

Shell Programming (contd.)

Test command

str1=“Good”

str2=“bad”

str3=

[$str1 = $str2 ]

echo$?

[$str1 != $str2]

echo$?

[-n $str1 ]

echo$?

[-z “$str3” ]

echo $?

[-z $str3 ]

echo$?

[ “Str3” ]

echo$?

Page 49: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 49

Shell Programming (contd.)

Test command If there are more than two words in a string

They have to be enclosed in double quotes for comparison

str1=“Hello World”

str2=“Goodbye World”

[“$str1” = “$str2” ]

#[$str1 = $str2 ] is an error

echo$?

Page 50: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 50

Shell Programming (contd.)

Test commandFile test

Operator Meaning

-s file True if file exist & greater than zero

-f file True if file exist & is not a directory

-d file True if file exist & is a directory

-c file True if file exist &

is a character special file

-b file True if file exist & block special file

Page 51: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 51

Shell Programming (contd.)

Test commandFile test

Operator Meaning

-r file True if file exist & has read permissions

-w file True if file exist & has write permissions

-x file True if file exist &

has execute permissions

-k file True if file exist &

its sticky bit is set

Page 52: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 52

Shell Programming (contd.)

File test# prgname

# To test if the file has write permission

echo Enter file name

read filename

if [ -w $filename ]

then

echo Type to append it to the file, Ctrl + D to stop

cat >> $filename

else

echo Sorry You don’t have the permission

fi

Page 53: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 53

Shell Programming (contd.)

Nested if - else

The if statement can be nested

if control commandthen

do thisand this

elseif control commandthen

do thiselse

do thisfi

fi

Page 54: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 54

Shell Programming (contd.)

Nested if - elseif control command

then

if control command

then

do this

else

do this

and this

fi

else

do this

fi

Page 55: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 55

Shell Programming (contd.)

if-then-elif-else-fi# example of nested else using the elif clause

echo “Enter the your name \c”

read name

if [ “$name” = sam ]

then

echo hello sam

elif [ “$name” = tom ]

then

echo hello tom

else

echo Sorry you do not have permissions

fi

Page 56: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 56

Shell Programming (contd.)

Logical operators

Operator Meaning

-a AND operator

-o OR operator

! NOT operator

Page 57: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 57

Shell Programming (contd.)

Logical operators# example logical operatorsecho “Enter any file name \c”read fnameif [ ! -z “$fname” ]then

if [ -r $fname -a -w $fname -a -x $fname ]echo read,write and execute permissions allowed

elseecho read,write and execute permissions denied

fielse

echo Improper file namefi

Page 58: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 58

Shell Programming (contd.)

Case control statementUsed to select form several possible alternatives

case value inchoice1)

do this;;

choice2)do this;;

*)do this;;

esac

Page 59: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 59

Shell Programming (contd.)

Case control statement# example case - esac controlecho “Enter 1 2 or 3: \c”read num

case $num in1)echo you entered one

;;2)echo you entered two

;;3)echo you entered three

;;*)echo only 1 or 2

;;esac

Page 60: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 60

Shell Programming (contd.)

Case control statement# Usage: prgname <name >

# name sam tom harry john

case $1 in

sam | tom) echo You have permissions to read only

;;

harry | john) echo You can read and write

;;

*)echo Permission denied

;;

esac

Page 61: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 61

Shell Programming (contd.)

Case control statementecho Enter any alphabet

read char

case $char in

[a-z])echo You entered lower case letter

;;

[A-Z])echo You entered Upper case letter

;;

?)echo You have entered a special symbol

;;

*)echo error

;;

esac

Page 62: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 62

Shell Programming (contd.)

case - esac statementThe case can be in any order

The value portion of the case statement can be A shell variable

A shell script argument

Out put of a command

Example case ‘who am i | cut -f1’ in

There cannot be a choice in case

$i -gt 50) # error

Page 63: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 63

Shell Programming (contd.)

ExercisesAn integer is entered through a key board

Find out if it is odd or even

Write a shell script which receives any year Determine if it is a leap year or not Hint A year is a leap year

If it is divisible 4 but not by 100except that years are divisible by 400

Write a shell script that accepts the login name Displays the terminal the user has logged onAppend this information in a log file

Page 64: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 64

Shell Programming (contd.)

Exercises

Write a shell script that displays messages

Good morning / afternoon / evening

Write a menu driven program that has the following

menus

Contents of /etc/passwd file

List of all the users who have logged on

Present working directory

The data and time in a proper format

exit

Page 65: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 65

Shell Programming (contd.)

Loop control

To repeat a set of instructions either

A specified number of times or

Until a particular condition occurs

The shell provides three methods

for statement

while statement

until statement

Page 66: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 66

Shell Programming (contd.)

Loop control# calculation of simple interest

count=1

while [ $count -le 3 ]

do

echo “\nEnter p,n,r\c”

read p n r

si=‘echo $p \* $n \* $r /100 | bc‘

echo Simple interest = Rs.$si

count=‘expr $count + 1‘

done

Page 67: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 67

Shell Programming (contd.)

Loop control

The control command can be any of the valid unix

command

while [ $# -le 5 ]

while who | grep $logname

while [ -r $file -a -w $file ] etc . . .

The statements with in the loop may be

A single command

Group of commands

They must be put in between do and done

Page 68: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 68

Shell Programming (contd.)

Loop control

The loop counter can be a real number

count=20.0

while [ $count -ge 10.0 ]

do

# do this

count=‘echo $count -0.1 | bc‘

done

Page 69: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 69

Shell Programming (contd.)

Loop control

Example

# usage prname <username>

if [ $# -lt 1 ]

then

echo Improper usage

echo correct usage is: $0 username

exit

fi

logname=$1

time=0

Page 70: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 70

Shell Programming (contd.)

Loop control

while truedo

who | grep “$logname” > /dev/nullif[$? = 0 ]then

echo $logname has logged inif[$time -ne 0 ]

then echo He has logged $time min latefi

exitelse

time=‘expr $time + 1‘ sleep 60

fidone

Page 71: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 71

Shell Programming (contd.)

Internal Field separator ( IFS )

It is a system variable

By default it value is space, tab and a new line

The IFS can be changed to any value

The program must reset it to the default value

Example

To read the contents of /etc/passwd file

The field separator used is :

Page 72: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 72

Shell Programming (contd.)

Internal Field separator ( IFS )# other lines for error checkinglogname=$1line=‘grep $logname /etc/passwd`oldifs=“$IFS”IFS=:set $lineclearecho User = $1echo UID = $3echo GID = $4echo Default working directory = $6

Page 73: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 73

Shell Programming (contd.)

until loopExecutes the instructions between do and done till

The exit status of the command is false Terminates when it becomes true

# printing numbers using while

i=1

while [ $i -le 10 ]

do

echo $1

i=‘expr $i + 1‘

done

Page 74: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 74

Shell Programming (contd.)

until loop

# printing numbers using until

i=1

until [ $i -gt 10 ]

do

echo $1

i=‘expr $i + 1‘

done

Page 75: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 75

Shell Programming (contd.)

for loopAllows to specify a list of values which the

Control variable can take

The loop is executed for each value in the list

for control-variable in value1 value2 value3. . .

do

command1

command2

command3

done

Page 76: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 76

Shell Programming (contd.)

for loopfor name in sam tom john kate

do

echo $name

done

sam

tom

john

kate

Page 77: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 77

Shell Programming (contd.)

for loop for command line arguments# $* can also be used as - for words in $*

for words

do

echo $words

done

# To print names of all sub-directoriesfor entry in *do

if[ -d $entry ]then

echo $entryfi

done

Page 78: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 78

Shell Programming (contd.)

for loopThe values that the control variable takes can

Be mentioned immediately after the in keyword Taken from the shell script argument

for var in $* or for var Take filenames from a directory

for file in *.c

do

mv $file $file.cpp

done

Page 79: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 79

Shell Programming (contd.)

for loop

The control variable can take the values from a shell

name=“sam tom john tim kate”

for words in $name

do

echo $words

done

Page 80: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 80

Shell Programming (contd.)

for loop

The control variable can take values

From the output of a command

for cmd in ‘cat commandlist‘

do

man $cmd >>helpfile

done

Page 81: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 81

Shell Programming (contd.)

break statementWhen the key word break is encountered the control

Automatically passes to the first statement after the loop

i=1 j=1while [ $i -le 100 ]do

while [ $j -lt 200 ]do

if [ $j -eq 150 ]break # break2 to break from outer loop

fij=‘expr $j + 1‘

donej=‘expr $j + 1‘

done

Page 82: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 82

Shell Programming (contd.)

continue statement

When the key word continue is encountered the control

Automatically passes to the beginning of the loop

Both break and continue can be used for

While loops

for loops

until loops

If there are three nested loops

continue3 will take the control to the outermost loop

Page 83: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 83

Shell Programming (contd.)

Exercise Write a program to count the number of

lines and words supplied at standard input

Write a program that takes a pathname as input Creates the directories if not already present Changes to the last directory in the list

Two numbers are entered, Write a shell script to find The value of one number raised to the power of other number

Write shell scripts that works similarly to head command tail command and more command

Page 84: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 84

Shell Programming (contd.)

ExerciseWrite a program to print all the prime numbers

Between 1 to 500 Use break and continue

Write a program to generate all possible combinations of

a b cWrite a script to print the list of files you have with

Only Read permissions Read and write permissions Read write and execute permissions

It must be a menu driven program

Page 85: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 85

Shell Programming (contd.)

Metatcharacters

Characters with special significance

Also called as regular expressions

Type Metacharacters

Filename substitution ? * [...] [!...]

I/O redirection > < >> << m> m>&n

Process execution ; ( ) & && ||

Quoting metacharacters \ “ “ ‘ ‘ ` `

Positional parameters $1...$9

Special Characters $0 $* $@ $# $! $$ $-

Page 86: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 86

Shell Programming (contd.)

Metatcharacters

File Name Substitution

? Stands for any one character

* A wild card character

Represents any combination of any number of characters

When mentioned with any command it represents

- A complete list of all files in current directory

- Except Hidden files that start with a period ( . )

Page 87: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 87

Shell Programming (contd.)

Metatcharacters

File Name Substitution

[...] The shell has a choice of any one character

from the list

[!...] The shell has a choice of any one characters

not in list

Examples

ls a* ls ??

ls a?b? ls [kdpe]*

ls [c-fmpv-z]* ls [!d-g]*

Page 88: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 88

Shell Programming (contd.)

Metatcharacters

I/O Redirection

Specify from where the input must be picked up and sent

Examples

cat file1 > out_file cat file2 >> out_file

cat < file2 >> out_file cat << stop

2 > errors time ls > myfile 2 > &1

Page 89: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 89

Shell Programming (contd.)

Metatcharacters

Process execution

Example

ls -l ; who ; banner Hello ( cd mydir ; pwd )

sort file1 > file2 & command1 && command2

command1 || command2

$ grep sam file1 || grep sam addfile && cat a file

Page 90: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 90

Shell Programming (contd.)

Metatcharacters

Quoting

\ Takes away the special significance of the

character

‘ ‘ (single quotes )Takes every enclosed character

literally

` ` (back quotes ) Replaces the command they

enclosed with its out put

“ “ Certain metacharacters are valid inside the quote

$ \ ` `

Page 91: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 91

Shell Programming (contd.)

Metatcharacters

Shell variables

$1 to $9 Shell variables

$$ PID of the current shell

$? Exit status of the last executed

command

$! PID of the last background process

$- Current Shell settings

Page 92: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 92

Shell Programming (contd.)

Metatcharacters

Shell variables

$# Total number of Positional parameters

$0 Name of the command being executed

$* List of all shell arguments

$@ Similar to $* , Yields each argument

separately when enclosed in double quotes

$! PID of the last background process

Page 93: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 93

Shell Programming (contd.)

Metatcharacters

Shell variables

# pgrname file1 file2 file3 . . .cat “$*”cat “$@”

pgrname f1 f2 f3

cat “f1 f2 f3”

cat “f1” “f2” “f3”

Page 94: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 94

Shell Programming (contd.)

Metatcharacters

Debugging a Script

Add the the following statement at the beginning of the script

set -vx

v Ensures that each line is displayed before

execution

x Ensures that command along with that

argument value is also displayed

$ echo $- Displays which options are set

set + vx Unsets the options

Page 95: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 95

Shell Programming (contd.)

getopts command

# Usage prgname [-a/b ]

getopts ab choice

case $choice in

a)echo You entered a

;;

b)echo You entered b

;;

?)echo error

Page 96: Shell Programming Ver 4.0 1  Objectives  Introduction to Shells  Introduction to shell programming  Shell variables  Arithmetic Operations  Conditions.

Shell Programming Ver 4.0 96

Shell Programming (contd.)

getopts command

On execution of the script

Accepts a single character option from the command line

The option must be preceded by - (minus sign )

getopts reads the options and decides if it is valid or not

If found valid it stores the value in the variable

If found invalid stores a “ ? “ in the variable

getopts also flashes an error message