introduction to unix (ca263) passing arguments
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 PresentationTRANSCRIPT
![Page 1: Introduction to Unix (CA263) Passing Arguments](https://reader036.vdocument.in/reader036/viewer/2022062309/56815084550346895dbe8031/html5/thumbnails/1.jpg)
Introduction to Unix (CA263)
Passing Arguments
By
Tariq Ibn AzizDammam Community College
![Page 2: Introduction to Unix (CA263) Passing Arguments](https://reader036.vdocument.in/reader036/viewer/2022062309/56815084550346895dbe8031/html5/thumbnails/2.jpg)
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
![Page 3: Introduction to Unix (CA263) Passing Arguments](https://reader036.vdocument.in/reader036/viewer/2022062309/56815084550346895dbe8031/html5/thumbnails/3.jpg)
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.
![Page 4: Introduction to Unix (CA263) Passing Arguments](https://reader036.vdocument.in/reader036/viewer/2022062309/56815084550346895dbe8031/html5/thumbnails/4.jpg)
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)
![Page 5: Introduction to Unix (CA263) Passing Arguments](https://reader036.vdocument.in/reader036/viewer/2022062309/56815084550346895dbe8031/html5/thumbnails/5.jpg)
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$
![Page 6: Introduction to Unix (CA263) Passing Arguments](https://reader036.vdocument.in/reader036/viewer/2022062309/56815084550346895dbe8031/html5/thumbnails/6.jpg)
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:
![Page 7: Introduction to Unix (CA263) Passing Arguments](https://reader036.vdocument.in/reader036/viewer/2022062309/56815084550346895dbe8031/html5/thumbnails/7.jpg)
$# 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=::
![Page 8: Introduction to Unix (CA263) Passing Arguments](https://reader036.vdocument.in/reader036/viewer/2022062309/56815084550346895dbe8031/html5/thumbnails/8.jpg)
$# 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=::
![Page 9: Introduction to Unix (CA263) Passing Arguments](https://reader036.vdocument.in/reader036/viewer/2022062309/56815084550346895dbe8031/html5/thumbnails/9.jpg)
$# Example[3]
• Pass the contents if names$ args `cat names`7 arguments passedarg 1 =:fil1: arg 2=:fil2: arg 3=:fil3:$
![Page 10: Introduction to Unix (CA263) Passing Arguments](https://reader036.vdocument.in/reader036/viewer/2022062309/56815084550346895dbe8031/html5/thumbnails/10.jpg)
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:
![Page 11: Introduction to Unix (CA263) Passing Arguments](https://reader036.vdocument.in/reader036/viewer/2022062309/56815084550346895dbe8031/html5/thumbnails/11.jpg)
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:
![Page 12: Introduction to Unix (CA263) Passing Arguments](https://reader036.vdocument.in/reader036/viewer/2022062309/56815084550346895dbe8031/html5/thumbnails/12.jpg)
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
![Page 13: Introduction to Unix (CA263) Passing Arguments](https://reader036.vdocument.in/reader036/viewer/2022062309/56815084550346895dbe8031/html5/thumbnails/13.jpg)
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
![Page 14: Introduction to Unix (CA263) Passing Arguments](https://reader036.vdocument.in/reader036/viewer/2022062309/56815084550346895dbe8031/html5/thumbnails/14.jpg)
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$
![Page 15: Introduction to Unix (CA263) Passing Arguments](https://reader036.vdocument.in/reader036/viewer/2022062309/56815084550346895dbe8031/html5/thumbnails/15.jpg)
Phonebook
$ cat phonebookAlice Chebba 596-2015Bob Swingle 598-9257Liz Stachiw 775-2298Susan Goldberg 338-7776Susan Topple 243-4932Tony Iannino 386-1295Tariq Aziz 230-4958
$
![Page 16: Introduction to Unix (CA263) Passing Arguments](https://reader036.vdocument.in/reader036/viewer/2022062309/56815084550346895dbe8031/html5/thumbnails/16.jpg)
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$
![Page 17: Introduction to Unix (CA263) Passing Arguments](https://reader036.vdocument.in/reader036/viewer/2022062309/56815084550346895dbe8031/html5/thumbnails/17.jpg)
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$
![Page 18: Introduction to Unix (CA263) Passing Arguments](https://reader036.vdocument.in/reader036/viewer/2022062309/56815084550346895dbe8031/html5/thumbnails/18.jpg)
Remove from Phonebook
$ cat rem# # remove someone to the phone book# grep –v "$1" phonebook >/tmp/phonebookmv /tmp/phonebook phonebook$
![Page 19: Introduction to Unix (CA263) Passing Arguments](https://reader036.vdocument.in/reader036/viewer/2022062309/56815084550346895dbe8031/html5/thumbnails/19.jpg)
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
![Page 20: Introduction to Unix (CA263) Passing Arguments](https://reader036.vdocument.in/reader036/viewer/2022062309/56815084550346895dbe8031/html5/thumbnails/20.jpg)
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
![Page 21: Introduction to Unix (CA263) Passing Arguments](https://reader036.vdocument.in/reader036/viewer/2022062309/56815084550346895dbe8031/html5/thumbnails/21.jpg)
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$
![Page 22: Introduction to Unix (CA263) Passing Arguments](https://reader036.vdocument.in/reader036/viewer/2022062309/56815084550346895dbe8031/html5/thumbnails/22.jpg)
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
![Page 23: Introduction to Unix (CA263) Passing Arguments](https://reader036.vdocument.in/reader036/viewer/2022062309/56815084550346895dbe8031/html5/thumbnails/23.jpg)
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