Introduction to Unix (CA263) Passing Arguments

Preview:

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

Introduction to Unix (CA263)

Passing Arguments

By

Tariq Ibn AzizDammam 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 phonebookRequest id is laser1-15 (standard input)

Example[2]

$ whoroot tty02 Jul 7 08:37fred tty03 Jul 8 08:30tony tty04 Jul 8 08:17lulu tty05 Jul 8 08:27taziz tty06 Jul 8 08:57ahmed tty07 Jul 8 08:47

$ cat isonwho | grep $1

•Execute it with taziz as the argument

$ ison taziztaziz 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 argsecho $# arguments passedecho arg 1 =:$1: arg 2=:$2: arg 3=:$3:$ args a b c3 arguments passedarg 1 =:a: arg 2=:b: arg 3=:c:

$# Example[1]

$ args a b2 arguments passedarg 1 =:a: arg 2=:b: arg 3=::$ args0 arguments passedarg 1 =:: arg 2=:: arg 3=::$ args "a b c"1 arguments passedarg 1 =:a b c: arg 2=:: arg 3=::

$# Example[2]

• See what files start with x$ ls x*xact xtra$ args x*2 arguments passedarg 1 =:xact: arg 2=:xtra: arg 3=::$ my_bin=/usr/steve/bin$ args $my_bin1 arguments passedarg 1 =:/usr/steve/bin: arg 2=:: arg 3=::

$# Example[3]

• Pass the contents if names$ args `cat names`7 arguments passedarg 1 =:fil1: arg 2=:fil2: arg 3=:fil3:$

The $* Variable

• The special variable $* references all arguments passed to the program.

$ cat args2echo $# arguments passedecho they are :$*:$ args a b c3 arguments passedthey are :a b c:

The $* Examples [1]

$ args one two2 arguments passedthey are :one two:$ args0 arguments passedthey are ::$ args *7 arguments passedthey 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: can’t open Tphonebook: Susan Goldberg 338-7776phonebook: 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 TariqTariq Aziz 230-4958$

Phonebook

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

$

Add in Phonebook Examples [1]

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

Phonebook

$ cat phonebookAlice Chebba 596-2015Bob Swingle 598-9257Liz Stachiw 775-2298Susan Goldberg 338-7776Susan Topple 243-4932Tony Iannino 386-1295Tariq Aziz230-4958Billy Bach331-7618$

Remove from Phonebook

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

Remove from Phonebook Example[1]

$ rem 'Tariq Aziz'$ cat phonebookAlice Chebba 596-2015Bob Swingle 598-9257Liz Stachiw 775-2298Susan Goldberg 338-7776Susan Topple 243-4932Tony Iannino 386-1295Billy Bach 331-7618$

$ rem 'Susan'$ cat phonebookAlice Chebba 596-2015Bob Swingle 598-9257Liz Stachiw 775-2298Tony Iannino 386-1295Billy 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 tshiftecho $# $*shiftecho $# $*shiftecho $# $*shiftecho $# $*shiftecho $# $*shiftecho $# $*$

$ tshift a b c d e5 a b c d e4 a b c d 3 a b c 2 a b 1 a0$

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

shiftshiftshift

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=$1shiftarg10=$9• Remember after executing shift command $1

contains the value of $2

Recommended