home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / unix / shell / 4638 < prev    next >
Encoding:
Text File  |  1992-11-10  |  1.3 KB  |  36 lines

  1. Newsgroups: comp.unix.shell
  2. Path: sparky!uunet!coplex!trebor!root
  3. From: root@trebor.uucp (Bob Stockler)
  4. Subject: Re: how to get the last pos. parameter in ksh?
  5. Organization: Bob Stockler
  6. Date: Wed, 11 Nov 1992 01:07:20 GMT
  7. Message-ID: <1992Nov11.010720.498@trebor.uucp>
  8. References: <KURO.92Nov9112643@vodka.Eng.Sun.Com>
  9. Lines: 25
  10.  
  11. kuro@vodka.Eng.Sun.Com (Teruhiko Kurosaka) writes:
  12.  
  13. >How can I write a ksh function to examine the last parameter (argument)
  14. >of the command line?  I.e., I'd like to know how to access argv[argc-1].
  15.  
  16. Off the top of my head, this oughta do something like you're after:
  17.  
  18.   function getlastarg
  19.     {
  20.       shift $(( $# - 1 ))
  21.       print $1
  22.     }
  23.   print $(getlastarg $@)
  24.  
  25. As I see it, the call of the function passes all of the command line
  26. arguments (or otherwise-set positional parameters) to the function
  27. "getlastarg", which (in my version of the KornShell) uses them as
  28. local (to it) positional parameters.  After shifting to the last one,
  29. the function returns it, which we read by command substitution.  The
  30. parent shell's positional parameters haven't been disturbed.
  31.  
  32. I am just learning the mysteries of the KornShell, so am posting this -
  33. rather than responding to you via email - in hopes to learn more about
  34. the KorenShell from those more experienced and knowledgeable.
  35.  
  36.