CopyToSD 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #!/bin/sh
  2. # copy files to SD card
  3. ERROR()
  4. {
  5. echo error: $*
  6. exit 1
  7. }
  8. USAGE()
  9. {
  10. [ -z "$1" ] || echo error: $*
  11. echo usage $(basename "$0") '[--sdcard=/media/disk]' '<files...>'
  12. exit 1
  13. }
  14. CopyFiles()
  15. {
  16. local item mountpoint src dst
  17. mountpoint=$1; shift
  18. for item in "$@"
  19. do
  20. item="${item}:"
  21. src="${item%%:*}"
  22. item="${item#*:}:"
  23. dst="${item%%:*}"
  24. item="${item#*:}:"
  25. cp "${src}" "${mountpoint}/${dst}"
  26. done
  27. ls -l "${mountpoint}"
  28. }
  29. SpinCount=0
  30. Spinner()
  31. {
  32. SpinCount=$((${SpinCount} + 1))
  33. case "${SpinCount}" in
  34. 1)
  35. printf -- '|\x08'
  36. ;;
  37. 2)
  38. printf -- '/\x08'
  39. ;;
  40. 3)
  41. printf -- '-\x08'
  42. ;;
  43. 4)
  44. printf -- '\\\x08'
  45. SpinCount=0
  46. ;;
  47. esac
  48. }
  49. InsertCard()
  50. {
  51. local dev mountpoint fstype
  52. dev="$1"; shift
  53. mountpoint="$1"; shift
  54. fstype=vfat
  55. umount "${mountpoint}" "${dev}" 2> /dev/null
  56. echo -n 'Insert SD Card '
  57. while [ ! -b "${dev}" ]
  58. do
  59. Spinner
  60. sleep 1
  61. done
  62. echo
  63. fsck.vfat -a "${dev}"
  64. mount -t "${fstype}" "${dev}" "${mountpoint}" || ERROR mount failed
  65. rm -f "${mountpoint}"/fsck*.rec
  66. df -h "${mountpoint}"
  67. }
  68. RemoveCard()
  69. {
  70. local dev mountpoint fstype
  71. dev="$1"; shift
  72. mountpoint="$1"; shift
  73. umount "${mountpoint}" || ERROR umount failed
  74. sleep 1
  75. echo -n ""
  76. echo 'SD Card Unmounted'
  77. echo
  78. }
  79. main()
  80. {
  81. dev=/dev/sdb1
  82. mountpoint=/mnt
  83. InsertCard "${dev}" "${mountpoint}"
  84. CopyFiles "${mountpoint}" "$@"
  85. df -h "${mountpoint}"
  86. RemoveCard "${dev}" "${mountpoint}"
  87. }
  88. sdcard=''
  89. # if a mounted sdcard is given then no need for sudo
  90. while [ $#0 -gt 0 ]
  91. do
  92. case "$1" in
  93. --sdcard=*)
  94. sdcard="${1#*=}"
  95. ;;
  96. --)
  97. shift
  98. break
  99. ;;
  100. -*)
  101. USAGE invalid option: $1
  102. ;;
  103. *)
  104. break
  105. ;;
  106. esac
  107. shift
  108. done
  109. if [ -d "${sdcard}" ]
  110. then
  111. CopyFiles "${sdcard}" "$@"
  112. exit
  113. fi
  114. # nee sudo to mount
  115. if [ -z "${SUDO_UID}" -o -z "${SUDO_GID}" ]
  116. then
  117. echo re-run with sudo
  118. sudo -K
  119. exec sudo "$0" "$@"
  120. exit 1
  121. fi
  122. if [ -n "${SUDO_UID}" -a -n "${SUDO_GID}" ]
  123. then
  124. main "$@"
  125. else
  126. echo sudo failed
  127. fi