"nmkdir" in the Bourne shell at a Unix machine near you.

Brian Matthews blm at cxsea.UUCP
Tue Aug 27 06:30:37 AEST 1985


> ** If you want it, it's yours.  All I ask in return is that if you
> ** figure out how to do this in a Bourne Shell script you send me
> ** a copy.

Here 'tis.  It basically uses expr to hack apart the requested path, and
do mkdir's on all the intermediate components.  I wrote it a while ago,
but haven't used it much, but I do think it works well.  There are three
known problems:

    1.  It's slow.
    2.  It doesn't trap any signals, so you can hit delete and have half
	of a path made.  This doesn't hurt anything, you may just have to
	do a little cleaning up.
    3.  It's slow.

>From the looks of the C source, it fixes all of the problems, so I may
switch to it and toss this.

I called it md, for Make Directory, kind of like Change Directory.

#!/bin/sh
#
# md directories - make all components in a given path
#
# written by Brian L. Matthews, 17-Jul-84
#
# md makes a directory path.  Unlike mkdir, it makes all necessary
# components of the path.  It uses mkdir, so it should fail and succeed in
# the same ways as mkdir.
# md tends to be slow, and should be recoded in C someday. Oh well...
#
if [ $# = 0 ]				# check for no arguments.  If none,
then					# print same stuff and use same
    echo "$0: arg count"		# exit status as mkdir
    exit 2
fi
while [ -n "$1" ]			# Loop through all arguments by
					# looking at the first and shifting
					# when done with it.
do
    made=				# $made tracks the components made
					# so far
    need=$1				# $need tracks what's left to make
    if [ `expr $need : '/.*'` = 0 ]	# if $need doesn't start with a slash,
    then				# add a slash, and say we've made
	made=`pwd`			# the current working directory.
	need=/$need
    fi
    while [ -n "$need" ]		# keep going until we've got it all
    do
	made=$made`expr $need : '\(.[^/]*\)/*'`
					# add the first component of $need to
					# $made.  If $made was /u and $need
					# was /blm, $made would become
					# /u/blm
	if [ ! -d "$made" -o ! -w "$made" ]
	then				# if $made isn't a directory or it's
	    mkdir $made			# not writable (i.e. it doesn't
	    if [ $? != 0 ]		# exist), do a mkdir.  If the mkdir
	    then			# fails, exit with it's exit code.
		exit $?			# mkdir has already printed the error.
	    fi
	fi
	need=`expr $need : '.[^/]*\(/*.*\)'`
					# remove the first component of $need.
    done				# we've now made a path, so scoot the
    shift				# arguments down, and do the next one.
done
exit 0


Brian L. Matthews
...uw-beaver!ssc-vax!cxsea!cxsea2!arrakis!blm



More information about the Comp.sources.unix mailing list