functions.sh 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #!/bin/bash
  2. PROGIMG="${0}"
  3. XIFS=$IFS
  4. #IFS=$' '
  5. selected=""
  6. options=""
  7. execution_premission()
  8. {
  9. local AMSURE
  10. if [ -n "${1}" ] ; then
  11. echo
  12. read -n 1 -p "${1} (y/[a]): " AMSURE
  13. else
  14. read -n 1 AMSURE
  15. fi
  16. echo "" 1>&2
  17. if [ "${AMSURE}" = "y" ] ; then
  18. return 0
  19. else
  20. return 1
  21. fi
  22. }
  23. decrunch()
  24. {
  25. test $# -gt 0 || { echo "${FUNCNAME[0]}: No params given"; return 1; }
  26. local params=($*)
  27. local params_count=${#params[*]}
  28. local src="${params[params_count-2]}"
  29. local dst="${params[params_count-1]}"
  30. #echo ${params} ${params_count} "src"=${src} "dst="${dst}
  31. local cmd=""
  32. if [ -f "${src}" ]; then
  33. case "${src}" in
  34. *.tar) cmd="tar -xp -C ${dst} ";;
  35. *.tar.bz2,*.tbz2) cmd="tar -xpj -C ${dst} ";;
  36. *.tar.gz,*.tgz) cmd="tar -xpz -C ${dst} ";;
  37. *.tar.xz) cmd="tar -xpJ -C ${dst} ";;
  38. *.bz2) cmd="bunzip2 > ${dst} ";;
  39. *.deb) cmd="ar x > {dst} ";;
  40. *.gz) cmd="gunzip > ${dst} ";;
  41. *.rar) cmd="unrar x ${dst} ";;
  42. *.rpm) cmd="rpm2cpio > ${dst} | cpio --quiet -i --make-directories ";;
  43. *.zip) cmd="unzip -d ${dst} ";;
  44. *.z) cmd="uncompress ${dst} ";;
  45. *.7z) cmd="7z x ${dst} ";;
  46. *) echo "${FUNCNAME[0]}: ${src} cannot be extracted via decrunch"; return 1;;
  47. esac
  48. pv ${src} | ${cmd}
  49. return $?
  50. else
  51. echo "${FUNCNAME[0]}: ${src} is not a valid file"
  52. return 1
  53. fi
  54. }
  55. download()
  56. {
  57. local url="${1}"
  58. local filename="${1##*\/}"
  59. local dst="${2}"
  60. local d_size="$(curl -sI ${url} | awk '/Content-Length/ { print $2 }')"
  61. curl -s -L -C - ${url} | pv -s ${d_size//[[:cntrl:]]/} > ${dst}/${filename}
  62. return $?
  63. }
  64. wait_umount()
  65. {
  66. test $# -gt 0 || { echo "${FUNCNAME[0]}: No params given"; return 1; }
  67. while $(mountpoint -q ${1}); do
  68. echo "Waiting for ${1} to unmount..."
  69. sleep 0.5
  70. done
  71. }
  72. cleanup()
  73. {
  74. test $# -gt 0 || { echo "${FUNCNAME[0]}: No params given"; return 1; }
  75. local cmd_ix=${#clean_cmds[*]}
  76. clean_cmds[$cmd_ix]=$@
  77. }
  78. proceed_cleanup()
  79. {
  80. local cmd_count=${#clean_cmds[*]}
  81. if [ ${cmd_count} -gt 0 ]; then
  82. execution_premission "Execute cleanup?" || return 1
  83. for cmd_ix in ${!clean_cmds[*]}
  84. do
  85. echo "${clean_cmds[cmd_count-cmd_ix-1]}"
  86. ${clean_cmds[cmd_count-cmd_ix-1]}
  87. done
  88. sync
  89. fi
  90. }
  91. prompt_select(){
  92. local exitcode
  93. options+=" exit"
  94. #echo ${options[@]} ${!cmds[@]} ${cmds[@]}
  95. printf "\n$*\n"
  96. select i in ${options}
  97. do
  98. [ -n "${i}" ] && { selected="${i}"; break; }
  99. done
  100. case ${selected} in
  101. "skip"|"") exitcode=1;;
  102. "exit") die "Exiting...";;
  103. *) exitcode=0;;
  104. esac
  105. return $exitcode
  106. }
  107. prompt_new_dir(){
  108. echo
  109. read -p "Enter name of new directory " new_dir
  110. try "mkdir -p ${@}/${new_dir}" && { echo ${@}/${new_dir}; return 0; } || return 1
  111. }
  112. save_var(){
  113. [ $# -eq 2 ] || { echo "${FUNCNAME[0]}: Two parameters expected! Get $#"; return 1; }
  114. echo "${1}="${!1} >> "${2}"
  115. [ -w "${2}" ] && { return 0; } || { echo "${2} is not valid file!"; return 1; }
  116. }
  117. check_dir()
  118. {
  119. if ! [ -d "${1}" ]; then
  120. if [ -z "${2}" ]; then
  121. echo "Directory ${1} doesn't exist."
  122. return 0
  123. else
  124. #echo "${2}" 1>&2
  125. return 1
  126. fi
  127. fi
  128. }
  129. is_mounted()
  130. {
  131. test $# -gt 0 || { echo "${FUNCNAME[0]}: No params given"; return 1; }
  132. [ -n "$(cat /proc/mounts | grep -q ${1})" ] && { return 0; } || { return 1; }
  133. }
  134. die()
  135. {
  136. test $# -gt 0 && echo "$(basename ${PROGIMG}): $@"
  137. proceed_cleanup
  138. IFS=$XIFS
  139. exit 0
  140. }
  141. try()
  142. {
  143. test $# -gt 0 || { echo "${FUNCNAME[0]}: No params given"; return 1; }
  144. printf "$*\n"
  145. $* || printf "\n${1} exit with error code $?\n"
  146. return $?
  147. }
  148. freespace()
  149. {
  150. test $# -gt 0 || { echo "${FUNCNAME[0]}: No params given"; return 1; }
  151. echo $(df -m -P ${1} | grep " ${1}$" | tail -n 1 | awk '{print $4}')
  152. return $?
  153. }
  154. prompt_format()
  155. {
  156. local params
  157. if execution_premission "Format partition ${1}? "; then
  158. options=$(find /sbin/* /usr/sbin/* -maxdepth 0 -name "mkfs.*")
  159. if prompt_select "Select filesystem"; then
  160. "${selected}"
  161. read -p "Enter additional params " params
  162. try "${selected} ${params} ${1}"
  163. fi
  164. fi
  165. return $?
  166. }
  167. fstabgen()
  168. {
  169. local mountpoint=
  170. local opt=
  171. local dump=
  172. local pass=
  173. local part=
  174. local id=
  175. for param in ${1}
  176. do
  177. IFS=':'
  178. read -r part mountpoint opt dump pass <<< "${param}"
  179. IFS=$XIFS
  180. #echo part=${part} mountpoint=${mountpoint} opt=${opt} dump=${dump} pass=${pass}
  181. if [ -n "${part}" ]; then
  182. part_uuid="$(blkid -o value -s PARTUUID ${part})"
  183. uuid="$(blkid -o value -s UUID ${part})"
  184. [ -n "${part_uuid}" ] && id="PARTUUID=${part_uuid}" || id="UUID=${uuid}"
  185. fstype="$(blkid -o value -s TYPE ${part})"
  186. echo -e "${id}\t${mountpoint}\t${fstype}\t${opt}\t${dump}\t${pass}" >> "${2}"
  187. fi
  188. done
  189. }
  190. get_partitions_list()
  191. {
  192. echo "$(find /dev/* -maxdepth 2 -name "sd??*" -or -name "hd??*" -or -name "loop1?*")"
  193. }