0% found this document useful (0 votes)
50 views23 pages

Introduction To Unix (CA263) Passing Arguments: by Tariq Ibn Aziz Dammam Community College

This document provides an introduction to passing arguments in Unix shell programs. It discusses special shell variables like $1 and $2 that store command line arguments, and $# which provides the number of arguments. Examples are given to demonstrate accessing arguments and using shift to change which argument is stored in each variable. The $* variable is also introduced as referencing all arguments. Additional examples demonstrate adding, removing, and looking up entries in a phonebook file.

Uploaded by

dprakash70
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views23 pages

Introduction To Unix (CA263) Passing Arguments: by Tariq Ibn Aziz Dammam Community College

This document provides an introduction to passing arguments in Unix shell programs. It discusses special shell variables like $1 and $2 that store command line arguments, and $# which provides the number of arguments. Examples are given to demonstrate accessing arguments and using shift to change which argument is stored in each variable. The $* variable is also introduced as referencing all arguments. Additional examples demonstrate adding, removing, and looking up entries in a phonebook file.

Uploaded by

dprakash70
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 23

Introduction to Unix (CA263) Passing Arguments

By Tariq Ibn Aziz Dammam Community College

Objectives
In this lecture you will learn
Shell variables Writing shell program Pass an arguments Add, remove and lookup into a file The shift command

Special Shell Variables


Whenever you execute a shell program, the shell automatically stores the first argument in the special shell variable 1, the second argument in the variable 2, and so on. The special variables as known as positional parameters.

Example[1]
c $ u r t n a l b t f o r n | m p T n u r x + d o m h c $ $

Execute it with phonebook as the argument

$ un phnbok r oe o Reqest i i s l r u d n a t ) u s e d a

s 5 ( i p

Example[2]
$ w
o r t y l u J 2 0 7 : r f d y t l u J 3 0 8 : o t y l u J 4 0 8 : u l y t J 5 0 8 : t i z a J : 7 8 t d e m h a J : 7 8

o$ s c i n o 1 $ p e r g | o h w Execute it with taziz as the argument


6 l 0 7 l 0

$
t 8 l u J 9 1 y 0 3 : i z

i
0

$ n a t $

The $# Variable
The shell variable $# gives you the number of arguments that were typed on the command line.
$ r a g c e s t n m u g r a # d h o $ e $ 3 a g r a : = 2 3 c b t p h r : 1 3 = $ a a g m n s p e 1 =

$# Example[1]
a $ a s b 2 a r a : = 2 g b $ a 0 a r a g : = 3 2 c b " s g r a $ 1 a a : g r = b g g m n s p e g 1 = : g g m n s p e g 1 = : g m n s p e = c r a 3

$# Example[2]
See what files start with x
$ s l c a x r t $ x s * a 2 a a r t x g : $ y e t r s u / = n i _ m b b i b _ y m s g r a $ 1 a a / : = n i b g r 2 s t x * g g m n s p e g 1 = x c : a 2 e g m n s p e g v

$# Example[3]
Pass the contents if names
$ s g r t a e ` 7 n e m u s a a f : = 2 l i 3 g r $ s c g p s e g

The $* Variable
The special variable $* references all arguments passed to the program.
a c $ g r e s t n m u g r a # d o h $ c e h $ a 3 a h t e

p h y a e : * g m n s p e r a b c

The $* Examples [1]


a $ e n o w 2 a h t e $ a 0 a h t e $ a
7 a t n 2 s g : e r a o h p b u x c y k a

g g m n s p e r o e t g g m n s p e g
g m n s p e e

The $* Examples [2]


$ c t l # # e i h p n L o k n p o # r g 1 $ e p p o e k $ T n a s S " u l $ r g a c e p : n t o e p 3 a s 8 o G d l k e o 7 u g p 2 a s 3 o T k e o 3 u $ Here, it passed 2 arguments not 1 argument

The $* Examples [3]


$ c t l # h i tep # L ksmeone u e n ho k oo p n o # e r g o h p k b " $ $ T n a s S " u l $ s3 u4 S2 e l p p n4 T3 $ Here, it passed " Su nT as sa " 1 argument

Add in Phonebook
c $ d a d # # t h e k o a m # o> h" c2 e$ e 1b $h "p $ a $ q i r T ' z 8 5 9 4 0 3 2 z l $ r a T q i a3 T2 r0 $

s e n o > n o d '

Phonebook
$ c A5 l9 ch1 C0 a b B5 o9 n7 i2 we l L7 ic8 a9 t2 w S 3 a s 8 S2 u4 ao3 T9 e l p T3 o8 yn5 a9 I2 i
a T i z A 3 2 8 5 9

t p o e k

Add in Phonebook Examples [1]


c $ d a d # # m o s d n h p e r v k 2 t i # c2 e$ h" r o S e n h p k b o $ a $ y l i B ' h c 8 6 7 1 3 a $ t a

o d '

Phonebook
$ c A5 l9 ch1 C0 a b B5 o9 n7 i2 we l L7 ic8 a9 t2 w Sn3 d l g7 o 6 a 8 s 7 G b S2 u4 ao3 T9 e l p T3 o8 yn5 a9 I2 i T2 a3 iz5 A9 8 B3 il6 a1 h8 $ t p o e k

Remove from Phonebook


t a c $ e r m # # r # g p v > k o b t e n h / $ " v m e n o h p / k b o $

e o t p

Remove from Phonebook Example[1]


$ r $ c A 0 2 6 b e h i C B 2 9 8 l g n b i L 2 5 i h c z a 6 7 8 3 o G n a s u S S 9 4 3 p o s T T 2 1 6 i n a B h c a l $ m$ 'S au ia z' s n t$ pc o e k A 0 2 6 b e h i C B 2 9 8 l g n b i L 2 5 i h c z a T 2 1 6 i n a B 7 h c 1 a 6 l $ In next lecture you will learn how to alert user if more than one match found r m ' t p o e k

1 6 7 -

The Shift Command


If you supply more than 9 arguments, there is no way to reference the argument 10 and up, because shell only accepts a single digit following the $ sign. $10 shell will actually substitute $1 followed by a 0. The shift command allow you to effectively left shift your positional parameters. If you execute the command shift then whatever stored in $2 will be assigned to $1, similarly $3 to $2 and so on. But value of $1 will be lost. When this command is executed, $# is also automatically decremented by one

The Shift Example


$ f s i t h h c e # $ * s h c e # $ * s h c e # $ * s h c e # $ * s h c e # $ * s h c e # $ * $ c $ t i f a t 5 c d i 4 t a c d 3 a i 2 t a 1 i 0 t $ i t i t

h a

b c b a

The Shift Example


If you try to shift when there are no variables to shift, then you will get the following error message. r p c t o n f p g name of the is rothe program that executed the shift. s i t 2 This above command has the same effect as performing 3 separate shift : n h s t f i s t f i s t f i

The Shift Example


If you really need to access the 10th argument, the easiest way is to execute the shift command, and then access the value as $9. You should save the value of $1 if you need later in the program. a r g = 1 s t f i a r g 0 $ Remember after executing shift command $1 contains the value of $2

You might also like