mkinstalldirs 1.8 KB

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