home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / gnu / bash / bug / 568 < prev    next >
Encoding:
Text File  |  1992-08-23  |  1.2 KB  |  64 lines

  1. Newsgroups: gnu.bash.bug
  2. Path: sparky!uunet!cis.ohio-state.edu!cs.mu.oz.AU!kre
  3. From: kre@cs.mu.oz.AU (Robert Elz)
  4. Subject: Re: Arguments for subroutines
  5. Message-ID: <9223701.4748@mulga.cs.mu.OZ.AU>
  6. Sender: gnulists@ai.mit.edu
  7. Organization: Computer Science, University of Melbourne, Australia
  8. References: <9208211821.AA02760@iowa.YP.solsys>
  9. Distribution: gnu
  10. Date: Sun, 23 Aug 1992 15:35:29 GMT
  11. Approved: bug-bash@prep.ai.mit.edu
  12. Lines: 50
  13.  
  14. In <9208211821.AA02760@iowa.YP.solsys> kburton@iowa.UUCP (Kevin Burton) writes:
  15.  
  16. | I have bash-1.12 and am trying to convert some CSH aliases to subroutines
  17. | in bash. I want to be able to get at all of the arguments from a specified
  18. | argument on (like !:3* in CSH).
  19.  
  20. Try ...
  21.  
  22. foo()
  23. {
  24.     echo $1
  25.     shift
  26.     echo $*
  27. }
  28.  
  29. or if you want $1 later
  30.  
  31. foo()
  32. {
  33.     X=$1
  34.     shift
  35.     echo $*
  36.     echo $X
  37. }
  38.  
  39. Of course, you can also use $@ instead of $* if that's more suited.
  40.  
  41. If you need $* put back (for some odd reason "$X $*" is not good enough)
  42. Then you can do
  43.  
  44. foo()
  45. {
  46.     ARGS=
  47.     while [ $# -gt 0 ]; do
  48.         ARGS="$ARGS '$1'"
  49.         shift
  50.     done
  51.     eval set -$- $ARGS
  52.  
  53.     X=$1
  54.     shift
  55.     echo $*        # to get the ${2:*} effect
  56.     shift
  57.     echo $*        # now ${3:*}
  58.  
  59.     eval set -$- $ARGS    # now all args are back as originally
  60. }
  61.  
  62. kre
  63.  
  64.