mkinstalldirs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #! /bin/sh
  2. # mkinstalldirs --- make directory hierarchy
  3. # Author: Noah Friedman <friedman@prep.ai.mit.edu>
  4. # Created: 1993-05-16
  5. # Public domain
  6. # $Id$
  7. errstatus=0
  8. dirmode=""
  9. usage="\
  10. Usage: mkinstalldirs [-h] [--help] [-m mode] dir ..."
  11. # process command line arguments
  12. while test $# -gt 0 ; do
  13. case "${1}" in
  14. -h | --help | --h* ) # -h for help
  15. echo "${usage}" 1>&2; exit 0 ;;
  16. -m ) # -m PERM arg
  17. shift
  18. test $# -eq 0 && { echo "${usage}" 1>&2; exit 1; }
  19. dirmode="${1}"
  20. shift ;;
  21. -- ) shift; break ;; # stop option processing
  22. -* ) echo "${usage}" 1>&2; exit 1 ;; # unknown option
  23. * ) break ;; # first non-opt arg
  24. esac
  25. done
  26. for file
  27. do
  28. if test -d "$file"; then
  29. shift
  30. else
  31. break
  32. fi
  33. done
  34. case $# in
  35. 0) exit 0 ;;
  36. esac
  37. case $dirmode in
  38. '')
  39. if mkdir -p -- . 2>/dev/null; then
  40. echo "mkdir -p -- $*"
  41. exec mkdir -p -- "$@"
  42. fi ;;
  43. *)
  44. if mkdir -m "$dirmode" -p -- . 2>/dev/null; then
  45. echo "mkdir -m $dirmode -p -- $*"
  46. exec mkdir -m "$dirmode" -p -- "$@"
  47. fi ;;
  48. esac
  49. for file
  50. do
  51. set fnord `echo ":$file" | sed -ne 's/^:\//#/;s/^://;s/\// /g;s/^#/\//;p'`
  52. shift
  53. pathcomp=
  54. for d
  55. do
  56. pathcomp="$pathcomp$d"
  57. case "$pathcomp" in
  58. -* ) pathcomp=./$pathcomp ;;
  59. esac
  60. if test ! -d "$pathcomp"; then
  61. echo "mkdir $pathcomp"
  62. mkdir "$pathcomp" || lasterr=$?
  63. if test ! -d "$pathcomp"; then
  64. errstatus=$lasterr
  65. else
  66. if test ! -z "$dirmode"; then
  67. echo "chmod $dirmode $pathcomp"
  68. lasterr=""
  69. chmod "$dirmode" "$pathcomp" || lasterr=$?
  70. if test ! -z "$lasterr"; then
  71. errstatus=$lasterr
  72. fi
  73. fi
  74. fi
  75. fi
  76. pathcomp="$pathcomp/"
  77. done
  78. done
  79. exit $errstatus
  80. # Local Variables:
  81. # mode: shell-script
  82. # sh-indentation: 3
  83. # End:
  84. # mkinstalldirs ends here