home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / unix / shell / 3469 < prev    next >
Encoding:
Text File  |  1992-08-12  |  2.8 KB  |  90 lines

  1. Newsgroups: comp.unix.shell
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!caen!hellgate.utah.edu!lanl!beta.lanl.gov!hall
  3. From: hall@beta.lanl.gov (Michael L. Hall)
  4. Subject: Escaping brackets in csh - SUMMARY
  5. Message-ID: <1992Aug12.234645.2589@newshost.lanl.gov>
  6. Sender: news@newshost.lanl.gov
  7. Organization: Los Alamos National Laboratory
  8. Date: Wed, 12 Aug 1992 23:46:45 GMT
  9. Lines: 79
  10.  
  11. I posted a question yesterday about renaming some VMS files that
  12. had the [dir1.dir2.dir3]filename.ext format into a unix format
  13. which retained the directory structure. I was wanting to do it 
  14. with a foreach (csh) or for (sh) command, but was having trouble
  15. escaping (or quoting) the brackets ([]). The best (or at least
  16. easiest for me to implement) answer was from Arne H. Juul - 
  17. <arnej@lise.unit.no>:
  18.  
  19. ------------------------------------
  20. This is my csh solution - 'do it with sh'!
  21.  
  22. csh% ls
  23. [asd.efg]gruff  [asd.hij]spiff  [asd.xcv]qwerty [bfd.zxc]foo    [jkl.bnm]bar
  24. csh% 
  25. csh% cat > rename.sh
  26. #!/bin/sh
  27.  
  28. /bin/ls \[* | while read filename; do
  29.     dir=`echo $filename | sed 's/^\[\(.*\)\].*/\1/' | tr . /`
  30.     dirs=`echo $filename | sed 's/^\[\(.*\)\].*/\1/' | tr . ' '`
  31.     file=`echo $filename | sed 's/^\[.*\]//'`
  32.     set $dirs
  33.     ( for x do mkdir $x 2>/dev/null; cd $x; done )
  34.     echo making $dir/$file
  35.     cp $filename $dir/$file
  36. done
  37. csh% sh rename.sh
  38. csh% ls
  39. asd/    bfd/    jkl/
  40. csh% 
  41. csh% find . -print
  42. .
  43. ./asd
  44. ./asd/efg
  45. ./asd/efg/gruff
  46. ./asd/hij
  47. ./asd/hij/spiff
  48. ./asd/xcv
  49. ./asd/xcv/qwerty
  50. ./bfd
  51. ./bfd/zxc
  52. ./bfd/zxc/foo
  53. ./jkl
  54. ./jkl/bnm
  55. ./jkl/bnm/bar
  56.  
  57.  
  58. There is a reason why csh isn't very well-liked in comp.unix.shell - and
  59. this is an example of the reason. Or to put it another way: I tried
  60. to make csh's foreach do it, but no way.
  61.  
  62. You could do the above in sh with
  63.     for filename in \[* ; do
  64. instead of the while, but the "ls | while read" is nicer.
  65. You could also do mv instead of cp in the shell-script above, of course.
  66. ---------------------------
  67.  
  68. Icarus Sparry gave me a similar answer, but I used the first one 
  69. because it was more easily deciphered. Many others suggested that
  70. I use the bourne shell, as brackets and fors mix much better than
  71. brackets and foreaches.
  72.  
  73. I am continually amazed at how people out in netland are willing to
  74. help you solve your problems.
  75.  
  76. Hearty thanks to:
  77.  
  78. Arne Henrik Juul <arnej@lise.unit.no>
  79. Icarus Sparry    <I.Sparry@sunlab1.bath.ac.uk>
  80. Watanabe Maki    <maki@jit.dec.com>
  81. Georg Wittig     <Georg.Wittig@gmd.de>
  82. Leo              <leo@ine.philips.nl>
  83. Tom Christiansen <tchrist@pixel.convex.com>
  84.                  
  85. -- 
  86. +----------------------------------------------------------------------+
  87. | Michael L. Hall    There are times, sir, when men of good conscience |
  88. | hall@lanl.gov      cannot blindly follow orders. - Jean-Luc Picard   |
  89. +----------------------------------------------------------------------+
  90.