home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / unix / shell / 3127 < prev    next >
Encoding:
Text File  |  1992-07-25  |  3.0 KB  |  83 lines

  1. Newsgroups: comp.unix.shell
  2. Path: sparky!uunet!gatech!usenet.ins.cwru.edu!odin!chet
  3. From: chet@odin.INS.CWRU.Edu (Chet Ramey)
  4. Subject: Re: GNU bash shell -- how to do csh alias things in bash functions
  5. Message-ID: <1992Jul24.204557.3222@usenet.ins.cwru.edu>
  6. Keywords: bash csh shell alias functions GNU
  7. Sender: news@usenet.ins.cwru.edu
  8. Nntp-Posting-Host: odin.ins.cwru.edu
  9. Organization: Case Western Reserve University, Cleveland, OH (USA)
  10. References: <1992Jul24.195840.11711@news.weeg.uiowa.edu>
  11. Date: Fri, 24 Jul 92 20:45:57 GMT
  12. Lines:       70
  13.  
  14. In article <1992Jul24.195840.11711@news.weeg.uiowa.edu> dkeber@news.weeg.uiowa.edu (David Keber) writes:
  15. >
  16. >My understanding is that you cannot put command line arguments into
  17. >bash aliases (right?), so I wrote the following two bash functions:
  18. >
  19. >    dir () { ls -aFglNqsT $1 | more ; }
  20. >    ls () { ls -CFq $1 | more ; }
  21. >
  22. >based of the example of functions given in the man page (btw/why the
  23. >semicolon at the end?).  THESE FUNCTIONS DO NOT WORK.  If I rename
  24. >the ls function to foo, then they both end up working correctly!
  25.  
  26. You've just written two infinitely recursive functions.  Aren't you
  27. proud ;-)
  28.  
  29. Functions are looked up before other commands, builtin and file system.
  30. Each time you call `ls', it finds the function, executes the body, finds
  31. that the first command is `ls', finds the function, ...
  32.  
  33. What you want to do is something like this:
  34.  
  35. dir() { command ls -aFglNqsT $1 | more ; }
  36. ls() { command ls -CFq $1 | more ; }
  37.  
  38. The `command' builtin executes its arguments as a command and arguments
  39. without including shell functions in the command search sequence.
  40.  
  41. >I want to do an
  42. >ls with the -CFq arguements almost all the time just by typing "ls".
  43.  
  44. Another thing you can do is
  45.  
  46. alias ls="ls -CFq"
  47.  
  48. and let the rest of the tokens on the line alone.  Bash takes special
  49. pains to avoid infinite recursion when expanding aliases.
  50.  
  51. >Another question I had was in converting the following alias:
  52. >
  53. >    alias whereis "find ~ -name '\!*' -print | more"
  54. >
  55. >the function I wrote was:
  56. >
  57. >    whereis() { find ~ -name $1 -print | more }
  58.  
  59. This works fine for me.  You can't enclose the `$1' in single quotes
  60. because this has the effect of inhibiting the expansion of $1, as
  61. single quotes are supposed to.
  62.  
  63. >is now, it just works unreliably.  In csh, this is supposed to give a
  64. >list of all files matching a specific pattern, for example
  65. >
  66. >          whereis *.ps
  67. >
  68. >would list the full pathname of any file that ended in ".ps".
  69.  
  70. "whereis '*.ps'" works fine for me.  Without the single quotes, globbing
  71. (pathname expansion) is performed before the function is called.  If you
  72. have files in the current directory ending with .ps, they become the
  73. results of the expansion and are passed to `whereis' as arguments.  If you
  74. happen to be in a directory with no ps files, the pattern is left unchanged
  75. and things work as you expect. 
  76.  
  77. Chet
  78. -- 
  79. ``The use of history as therapy means the corruption of history as history.''
  80.     -- Arthur Schlesinger
  81.  
  82. Chet Ramey, Case Western Reserve University    Internet: chet@po.CWRU.Edu
  83.