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

  1. Newsgroups: comp.unix.shell
  2. Path: sparky!uunet!munnari.oz.au!cs.mu.OZ.AU!glaze
  3. From: glaze@cs.mu.oz.au (Glaze)
  4. Subject: Re: Escaping brackets in csh - SUMMARY
  5. Message-ID: <glaze.093@glaze>
  6. Sender: news@cs.mu.oz.au
  7. Organization: Computer Science, University of Melbourne, Australia
  8. References: <1992Aug12.234645.2589@newshost.lanl.gov>
  9. Date: Thu, 13 Aug 1992 23:38:13 GMT
  10. Lines: 47
  11.  
  12. hall@beta.lanl.gov (Michael L. Hall) writes:
  13. >I posted a question yesterday about renaming some VMS files that
  14. >had the [dir1.dir2.dir3]filename.ext format into a unix format
  15. >which retained the directory structure. I was wanting to do it 
  16. >with a foreach (csh) or for (sh) command, but was having trouble
  17. >escaping (or quoting) the brackets ([]). The best (or at least
  18. >easiest for me to implement) answer was from Arne H. Juul - 
  19. ><arnej@lise.unit.no>:
  20.  
  21. [...delete area...]
  22. >This is my csh solution - 'do it with sh'!
  23. >#!/bin/sh
  24. >/bin/ls \[* | while read filename; do
  25. >    dir=`echo $filename | sed 's/^\[\(.*\)\].*/\1/' | tr . /`
  26. >    dirs=`echo $filename | sed 's/^\[\(.*\)\].*/\1/' | tr . ' '`
  27. >    file=`echo $filename | sed 's/^\[.*\]//'`
  28. >    set $dirs
  29. >    ( for x do mkdir $x 2>/dev/null; cd $x; done )
  30. >    echo making $dir/$file
  31. >    cp $filename $dir/$file
  32. >done
  33.  
  34. One thing you may need to do is change the ``mkdir'' to ``mkdir -p'' above.
  35. The -p option requesting that mkdir create any missing parent directories.
  36. However, older mkdir's don't support it, so here's a small script I
  37. whipped up for you:
  38.  
  39. #! /bin/sh
  40. # simulate a "mkdir -p"
  41. parent()
  42. {
  43.     if test \! -d $1 ; then
  44.         parent `dirname $1` 
  45.         mkdir `echo $1 | sed 's@/$@@'`
  46.     fi
  47. }
  48. usage()
  49. {
  50.     echo "mkdirp: Usage: mkdirp dirname"
  51.     exit 1
  52. }
  53. if test $# -ne 1 ; then
  54.     usage
  55. else
  56.     parent $1
  57. fi
  58. exit 0
  59.