home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.shell
- 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
- From: john@physiol.su.OZ.AU (John Mackin)
- Subject: recursive mkdir
- Message-ID: <1992Aug18.033048.7574@physiol.su.OZ.AU>
- Organization: Department of Physiology, University of Sydney, Australia
- References: <1992Aug12.234645.2589@newshost.lanl.gov> <glaze.093@glaze>
- Date: Tue, 18 Aug 1992 03:30:48 GMT
- Lines: 68
-
- In article <glaze.093@glaze> glaze@cs.mu.oz.au (Glaze) writes:
-
- > The -p option requesting that mkdir create any missing parent directories.
- > However, older mkdir's don't support it, so here's a small script I
- > whipped up for you:
-
- The provided script began with "#!/bin/sh", yet used shell functions.
-
- I get really tired of people who don't understand portability.
-
- I append to this message a _portable_ recursive mkdir written in plain
- vanilla sh. As an added bonus, it even has error checking! Gosh!
-
- --
- John Mackin <john@physiol.su.oz.au>
- `A Systems Programmer spends his life getting into tense situations.' -Boyd
-
- --- begin Mkdir
- #!/bin/sh
-
- name=`basename $0`
- case $# in
-
- 0) echo usage: $name directory ... >&2
- exit 1
- ;;
-
- 1) cwd=. ;; # no need to waste time
-
- *) cwd=`pwd` ;;
-
- esac
-
- IFS="/$IFS"
- for arg
- do
-
- case "$arg"
- in
- \/*) cd "/" ;;
- esac
-
- for d in $arg
- do
- if test ! -d $d
- then
- if mkdir $d
- then : fall through
- else
- echo $name: mkdir failed in directory "`pwd`"
- exit 1
- fi
- fi
-
- # if builtin cd $d
- if cd $d
- then : ok, go on
- else
- echo $name: cd failed in directory "`pwd`"
- exit 1
- fi
- done
-
- # builtin cd "$cwd"
- cd "$cwd"
-
- done
- --- end Mkdir
-