1 / 23

Introduction to Unix (CA263) Passing Arguments

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.

hollye
Download Presentation

Introduction to Unix (CA263) Passing Arguments

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Unix (CA263)Passing Arguments By Tariq Ibn Aziz Dammam Community College

  2. 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

  3. 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.

  4. 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)

  5. $ 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 $ Example[2]

  6. 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:

  7. $# 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=::

  8. $# 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=::

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

  10. 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:

  11. The $* Examples [1] $ args one two 2 arguments passed they are :one two: $ args 0 arguments passed they are :: $ args * 7 arguments passed they are :args args2 names nu phonebook stat xact xtra:

  12. The $* Examples [2] $ cat lu # # Look someone up in the phone book # grep $1 phonebook $ $ lu "Susan T" grep: can’t open T phonebook: Susan Goldberg 338-7776 phonebook: Susan Topple 243-4932 $ Here, it passed 2 arguments not 1 argument

  13. 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

  14. 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 $

  15. 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 $

  16. 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 $

  17. 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 $

  18. Remove from Phonebook $ cat rem # # remove someone to the phone book # grep –v "$1" phonebook >/tmp/phonebook mv /tmp/phonebook phonebook $

  19. $ 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 Remove from Phonebook Example[1]

  20. 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

  21. $ cat tshift echo $# $* shift echo $# $* shift echo $# $* shift echo $# $* shift echo $# $* shift echo $# $* $ $ tshift a b c d e 5 a b c d e 4 a b c d 3 a b c 2 a b 1 a 0 $ The Shift Example

  22. 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

  23. 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

More Related