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