You are on page 1of 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]
$ cat run tbl $1 |nroff mm Tlp |lp $ chmod +x run

Execute it with phonebook as the argument


$ run phonebook Request id is laser1-15 (standard input)

Example[2]
$ who
root tty02 Jul 7 08:37 fred tty03 Jul 8 08:30 tony tty04 Jul 8 08:17 lulu tty05 Jul 8 08:27 taziz tty06 Jul 8 08:57 ahmed tty07 Jul 8 08:47

$ cat ison who | grep $1 Execute it with taziz as the argument $ ison taziz
taziz tty19 Jul 8 08:30

$ ison taziz $

The $# Variable
The shell variable $# gives you the number of arguments that were typed on the command line.
$ cat args echo $# arguments passed echo arg 1 =:$1: arg 2=:$2: arg 3=:$3: $ args a b c 3 arguments passed arg 1 =:a: arg 2=:b: arg 3=:c:

$# Example[1]
$ args a b 2 arguments passed arg 1 =:a: arg 2=:b: arg 3=:: $ args 0 arguments passed arg 1 =:: arg 2=:: arg 3=:: $ args "a b c" 1 arguments passed arg 1 =:a b c: arg 2=:: arg 3=::

$# Example[2]
See what files start with x
$ ls x* xact xtra $ args x* 2 arguments passed arg 1 =:xact: arg 2=:xtra: arg 3=:: $ my_bin=/usr/steve/bin $ args $my_bin 1 arguments passed arg 1 =:/usr/steve/bin: arg 2=:: arg 3=::

$# Example[3]
Pass the contents if names
$ args `cat names` 7 arguments passed arg 1 =:fil1: arg 2=:fil2: arg 3=:fil3: $

The $* Variable
The special variable $* references all arguments passed to the program.
$ cat args2 echo $# arguments passed echo they are :$*: $ args a b c 3 arguments passed they are :a b c:

The $* Examples [1]


$ args one 2 arguments passed they are :one two: $ args 0 arguments passed they are :: $ args * two

7 arguments passed they are :args args2 names nu phonebook stat xact xtra:

The $* Examples [2]


$ cat lu # # Look someone up in the phone book # grep $1 phonebook $ $ lu "Susan T" grep: cant open T phonebook: Susan Goldberg 338-7776 phonebook: Susan Topple 243-4932 $ Here, it passed 2 arguments not 1 argument

The $* Examples [3]


$ cat lu # # Look someone up in the phone book # grep "$1" phonebook $ $ lu "Susan T" Susan Topple 243-4932 $ Here, it passed "Susan T" as 1 argument

Add in Phonebook
$ cat add # # add someone to the phone book # echo "$1 $2" >> phonebook $ $ add 'Tariq Aziz' 230-4958 $ lu Tariq Tariq Aziz 230-4958 $

Phonebook
$ cat phonebook Alice Chebba 596-2015 Bob Swingle 598-9257 Liz Stachiw 775-2298 Susan Goldberg 338-7776 Susan Topple 243-4932 Tony Iannino 386-1295
Tariq Aziz 230-4958

Add in Phonebook Examples [1]


$ cat add # # add someone to the phone book version 2 # echo "$1 $2" >> phonebook Sort o phonebook phonebook $ $ add 'Billy Bach' 331-7618 $

Phonebook
$ cat phonebook Alice Chebba 596-2015 Bob Swingle 598-9257 Liz Stachiw 775-2298 Susan Goldberg 338-7776 Susan Topple 243-4932 Tony Iannino 386-1295 Tariq Aziz 230-4958 Billy Bach 331-7618 $

Remove from Phonebook


$ cat rem # # remove someone to the phone book # grep v "$1" phonebook >/tmp/phonebook mv /tmp/phonebook phonebook $

Remove from Phonebook Example[1]


$ rem 'Tariq Aziz' $ cat phonebook Alice Chebba 596-2015 Bob Swingle 598-9257 Liz Stachiw 775-2298 Susan Goldberg 338-7776 Susan Topple 243-4932 Tony Iannino 386-1295 Billy Bach 331-7618 $ $ rem 'Susan' $ cat phonebook Alice Chebba 596-2015 Bob Swingle 598-9257 Liz Stachiw 775-2298 Tony Iannino 386-1295 Billy Bach 331-7618 $ In next lecture you will learn how to alert user if more than one match found

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


$ cat tshift echo $# $* shift echo $# $* shift echo $# $* shift echo $# $* shift echo $# $* shift echo $# $* $ $ 5 4 3 2 1 0 $ tshift a b c d e a b c d e a b c d a b c a 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. prog: cannot shift prog is the name of the program that executed the shift. shift 2 This above command has the same effect as performing 3 separate shift shift shift shift

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. arg1=$1 shift arg10=$9 Remember after executing shift command $1 contains the value of $2

You might also like