File: mkdir_p

package info (click to toggle)
super 3.30.0-3+squeeze2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 1,088 kB
  • ctags: 755
  • sloc: ansic: 10,089; sh: 288; makefile: 201
file content (50 lines) | stat: -rwxr-xr-x 1,384 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#! /bin/sh

usage() {
cat << END
A poor man's mkdir -p.  Useful on old systems that don't have the -p option.
It won't work right if a target directory includes whitespace; tough luck.
Options:
    -h     print this info, then exit.
    -oxxx  chown xxx any created directory (put no space between -o and xxx).
    -gxxx  chgrp xxx any created directory (put no space between -g and xxx).
    -pxxx  chperm xxx any created directory (put no space between -p and xxx).
END
}

owner=
group=
perm=
while [ $# -gt 0 ] ; do
    case "$1" in
    -h ) usage ; exit 0 ;;
    -o* ) owner=`expr "$1" : '-o\(.*\)'` ; shift
    	case "$owner" in "" ) usage ; exit 1 ;; esac
	;;
    -g* ) group=`expr "$1" : '-g\(.*\)'` ; shift
    	case "$group" in "" ) usage ; exit 1 ;; esac
	;;
    -p* ) perm=`expr "$1" : '-p\(.*\)'` ; shift
    	case "$perm" in "" ) usage ; exit 1 ;; esac
	;;
    -- ) break ;;
    -* ) echo "unknown option $1" ; usage ; exit 1 ;;
    * ) break ;;
    esac
done

for longpath in ${1+"$@"} ; do
    case "$longpath" in
    /* ) dir="" ;;
    * )  dir=. ;;
    esac
    for d in `echo "$longpath" | sed -e 'sX/X Xg'` ; do
	dir="$dir/$d"
	test -d $dir || {
	    mkdir $dir || exit
	    case "$owner" in "" ) ;; * ) chown $owner $dir ;; esac
	    case "$group" in "" ) ;; * ) chgrp $group $dir ;; esac
	    case "$perm" in "" ) ;; * ) chmod $perm $dir ;; esac
	}
    done
done