home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / unix / shell / 3772 < prev    next >
Encoding:
Internet Message Format  |  1992-09-02  |  1.9 KB

  1. Path: sparky!uunet!mcsun!uknet!siesoft!wstuart!stuart
  2. From: stuart@wstuart (Stuart Hood)
  3. Newsgroups: comp.unix.shell
  4. Subject: Re: alias problems
  5. Message-ID: <1992Sep2.184806.2497@siesoft.co.uk>
  6. Date: 2 Sep 92 18:48:06 GMT
  7. References: <1992Aug29.140835.15176@sfera.kharkov.ua>
  8. Sender: news@siesoft.co.uk (Usenet News)
  9. Reply-To: stuart@siesoft.co.uk
  10. Organization: Siemens Nixdorf Information Systems Ltd.
  11. Lines: 54
  12. X-Newsreader: Tin 1.1 PL4
  13.  
  14. mickey@sfera.kharkov.ua (Mickey Serg Antonoff) writes:
  15. : alias md "sh -c 'for f in \!*;do mkdir $f ; cd $f;done'"
  16. :   it have to do:
  17. :     make directory, than cd to it;
  18. :   call:
  19. :     cd ~
  20. :     md 1 2 3 4 5 6
  21. :   result:
  22. :     cwd = ~/1/2/3/4/5/6
  23.  
  24. This doesn't work because "sh -c" starts up a new process.
  25. New processes inherit their parent's environment (including
  26. "cwd") but cannot (sensibly) alter their parent's environment.
  27.  
  28. A common "trick" to get round this problem is for the child
  29. process to "echo" the result of its task and for the parent
  30. to use that output in some way.  Something like:
  31.  
  32.     cd `sh -c 'for f in 1 2 3 4 5 6; \
  33.         do mkdir $f; cd $f; echo -n $f/; done; echo'`
  34.  
  35. would change the "cwd" of the current process.  So an alias
  36. along the lines of:
  37.  
  38.     alias md cd \`sh -c \'for f in \!\* \; \
  39.         do mkdir \$f \; cd \$f \; echo -n \$f/ \; done \; echo\'\`
  40.  
  41. should do what is required.
  42.  
  43. Note: the use of '\' is needed to avoid the problem of:
  44.       "f": variable not found
  45.  
  46. This looks too messy to my mind so I would prefer to use
  47. "source" and a file.  Given the file:
  48.  
  49.     # $HOME/.md
  50.     foreach d ( $list )
  51.     mkdir $d
  52.     cd $d
  53.     end
  54.  
  55. then we can implement the simple alias:
  56.  
  57.     alias md 'set list = "\!*" ; source ~/data/md'
  58.  
  59. Stuart.
  60. -
  61. --
  62. S I E M E N S  Stuart Hood === Siemens Nixdorf House, Bracknell, Berks, UK
  63. -------------  Phone: + 44-344-850892          Email: stuart@siesoft.co.uk
  64. N I X D O R F  - Never let ignorance stand in the way of a good argument -
  65.