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

  1. Newsgroups: comp.unix.shell
  2. Path: sparky!uunet!munnari.oz.au!mel.dit.csiro.au!mineng.dmpe.CSIRO.AU!dmssyd.syd.dms.CSIRO.AU!metro!physiol.su.OZ.AU!john
  3. From: john@physiol.su.OZ.AU (John Mackin)
  4. Subject: recursive mkdir
  5. Message-ID: <1992Aug18.033048.7574@physiol.su.OZ.AU>
  6. Organization: Department of Physiology, University of Sydney, Australia
  7. References: <1992Aug12.234645.2589@newshost.lanl.gov> <glaze.093@glaze>
  8. Date: Tue, 18 Aug 1992 03:30:48 GMT
  9. Lines: 68
  10.  
  11. In article <glaze.093@glaze> glaze@cs.mu.oz.au (Glaze) writes:
  12.  
  13. > The -p option requesting that mkdir create any missing parent directories.
  14. > However, older mkdir's don't support it, so here's a small script I
  15. > whipped up for you:
  16.  
  17. The provided script began with "#!/bin/sh", yet used shell functions.
  18.  
  19. I get really tired of people who don't understand portability.
  20.  
  21. I append to this message a _portable_ recursive mkdir written in plain
  22. vanilla sh.  As an added bonus, it even has error checking!  Gosh!
  23.  
  24. -- 
  25. John Mackin <john@physiol.su.oz.au>
  26. `A Systems Programmer spends his life getting into tense situations.'  -Boyd
  27.  
  28. --- begin Mkdir
  29. #!/bin/sh
  30.  
  31. name=`basename $0`
  32. case $# in
  33.  
  34.     0) echo usage: $name directory ... >&2
  35.        exit 1
  36.        ;;
  37.  
  38.     1) cwd=. ;;        # no need to waste time
  39.  
  40.     *) cwd=`pwd` ;;
  41.  
  42. esac
  43.  
  44. IFS="/$IFS"
  45. for arg
  46. do
  47.  
  48.     case "$arg"
  49.     in
  50.     \/*) cd "/" ;;
  51.     esac
  52.  
  53.     for d in $arg
  54.     do
  55.     if test ! -d $d
  56.     then
  57.         if mkdir $d
  58.         then    : fall through
  59.         else
  60.         echo $name: mkdir failed in directory "`pwd`"
  61.         exit 1
  62.         fi
  63.     fi
  64.  
  65.     # if builtin cd $d
  66.     if cd $d
  67.     then : ok, go on
  68.     else
  69.         echo $name: cd failed in directory "`pwd`"
  70.         exit 1
  71.     fi
  72.     done
  73.  
  74.     # builtin cd "$cwd"
  75.     cd "$cwd"
  76.  
  77. done
  78. --- end Mkdir
  79.