home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / unix / shell / 3495 < prev    next >
Encoding:
Internet Message Format  |  1992-08-15  |  3.0 KB

  1. Path: sparky!uunet!autodesk!dansmith
  2. From: dansmith@Autodesk.COM (Daniel Smith)
  3. Newsgroups: comp.unix.shell
  4. Subject: Re: testing for arguments in aliases
  5. Message-ID: <17483@autodesk.COM>
  6. Date: 15 Aug 92 22:26:13 GMT
  7. References: <1992Aug9.041528.10720@jade.tufts.edu>
  8. Organization: Autodesk Inc., Sausalito CA, USA
  9. Lines: 69
  10.  
  11. In <1992Aug9.041528.10720@jade.tufts.edu> rdorich@jade.tufts.edu (Rob) writes:
  12.  
  13. > Hi there netters,
  14. >   No too long ago I started using arguments within aliases.  I was
  15. > wondering if some one could tell me how to test for an argument within
  16. > an alias.  eg. if we had:
  17. > alias foo 'test for 1 arg and do'
  18. > ...
  19.  
  20.     Ok, at this point, you're starting to want more functionality
  21. out of an alias than what can conveniently be jammed into one line.
  22. By "convenient" I mean: something you can go back to in a week and
  23. grasp/edit, without trying to recall every last one-liner trick in the
  24. book when you're in a hurry :-)
  25.  
  26.     Try passing the args to a variable, and then have the alias
  27. source a script.  With that you can spread the logic out over a few lines
  28. if need be.
  29.  
  30.     Here's an encore presentation of my newdir alias/script
  31. combo.  This checks for args, and makes a new directory.  When the directory
  32. is made, we move any other files/directories specified on the command
  33. line to it, and pushd there.  This ties together the 3 most common things
  34. I like to do when I make a new directory.
  35.  
  36.     The alias:
  37.  
  38. alias   newdir  'set newdir_attempt=(\!*); source ~/bin/newdir_csh'
  39.  
  40.     easy so far... $newdir_attempt will "pass" our args for us.
  41.  
  42.     The ~/bin/newdir_csh script which does the work:
  43.  
  44. #!/bin/echo sorry,try:source
  45. if ($#newdir_attempt == 0) then
  46.         echo 'usage: newdir dir_to_make [files to move to dir_to_make]'
  47.         goto nd_end
  48. endif
  49.  
  50. mkdir $newdir_attempt[1] || goto nd_end
  51. set cd_attempt=$newdir_attempt[1]
  52. if ($#newdir_attempt > 1) then
  53.         shift newdir_attempt
  54.         mv -f $newdir_attempt $cd_attempt
  55.         unset newdir_attempt
  56. endif
  57. pl $cd_attempt   # substitute your favorite pushd alias here...
  58.  
  59. nd_end:
  60.  
  61.     The $#newdir_attempt line at the top gives us a way of checking
  62. to see that there were some args.  We also do a check farther down to see
  63. if we should move any files/directories to the newly created directory.
  64.  
  65.     Note that by sourcing a script, you buy yourself the ability
  66. to change your csh environment, and you also have the opportunity
  67. to embed bits of perl or sh in a "here document", etc.  It's amazing
  68. what can be accomplished with a one-line alias, and I've written
  69. some real lulu's myself.  With the onset of age though (ripe old 31),
  70. I no longer want to take all afternoon debugging silly one-line csh things,
  71. and prefer "spelling it out" in a script.  I now reserve time for debugging
  72. silly lines of perl :-)
  73.  
  74.                 Daniel
  75. -- 
  76.             dansmith@autodesk.com            dansmith@well.sf.ca.us
  77.         Daniel Smith, Autodesk, Sausalito, California, (415) 332-2344 x 2580
  78.               Disclaimer: written by a highly caffeinated mammal
  79.  $|=1;for("..."," hacker"," perl"," a","just"){print $l=$_.$l,"\015";sleep 1;}
  80.