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

Stan Tazuma stan at tikal.UUCP
Wed Aug 28 06:32:38 AEST 1985


In article <309 at cxsea.UUCP> blm at cxsea.UUCP (Brian Matthews) writes:
>> ** 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.

Just to illustrate some possibly little known features of the
Bourne shell, I thought I'd followup with another version of nmkdir.
Expr and test can be slow enough that the use of the shell pattern
matching features (the case statement) may be preferred.  Also,
creative use of IFS can be a big help.  Here is my version:

#! /bin/sh
cwd=`pwd`
IFS=/
    # after setting IFS, anything with '/' has to be quoted.
    # also, IFS will take care of repeated '/'s properly.
for arg
{
    case "$arg"
    {
	/*) cd "/" ;;
    }

    for d in $arg
    {
	if test ! -d $d
	then
	    if mkdir $d
	    then	: fall through
	    else
		echo "Error making directory $d in dirpath $arg"
		exit 1
		#### break
	    fi
	fi

	if cd $d
	then : ok, go on
	else
	    echo "Error, can't chdir to $d in dirpath $arg"
	    exit 1
	fi
    }
    cd "$cwd"
}
exit



More information about the Comp.sources.unix mailing list