You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

progressbar.sh 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #!/bin/sh
  2. #
  3. # Show a progress bar.
  4. #
  5. # If kdialog/zenity are available they are used.
  6. # Progress data is read from the created pipe, echoing "quit" to the pipe will
  7. # cause the script to terminate, as will the progress reaching 100%
  8. #
  9. # Pressing cancel on a zenity dialog will terminate right away, kdialog will
  10. # only terminate when the next line of data is read from the pipe.
  11. #
  12. # Check the which command exists, and if so make sure it behaves how we want
  13. # it to...
  14. WHICH=`which which 2>/dev/null`
  15. if [ "" = "${WHICH}" ]; then
  16. echo "which command not found. Aborting.";
  17. exit 0;
  18. else
  19. # Solaris sucks
  20. BADWHICH=`which /`
  21. if [ "${BADWHICH}" != "" ]; then
  22. echo "Replacing bad which command.";
  23. # "Which" on solaris gives non-empty results for commands that don't exist
  24. which() {
  25. OUT=`${WHICH} ${1}`
  26. if [ $? -eq 0 ]; then
  27. echo ${OUT}
  28. else
  29. echo ""
  30. fi;
  31. }
  32. fi;
  33. fi
  34. # Solaris also has a crappy version of tail!
  35. tail -n +1 /dev/null >/dev/null 2>&1
  36. if [ $? -eq 2 ]; then
  37. TAILOPTS="-"
  38. else
  39. TAILOPTS="-n"
  40. fi;
  41. PIDOF=`which pidof`
  42. if [ "${PIDOF}" = "" ]; then
  43. # For some reason some distros hide pidof...
  44. if [ -e /sbin/pidof ]; then
  45. PIDOF=/sbin/pidof
  46. elif [ -e /usr/sbin/pidof ]; then
  47. PIDOF=/usr/sbin/pidof
  48. fi;
  49. fi;
  50. ## Helper Functions
  51. if [ "${PIDOF}" != "" ]; then
  52. ISKDE=`${PIDOF} -x -s kdeinit`
  53. ISGNOME=`${PIDOF} -x -s gnome-panel`
  54. else
  55. ISKDE=`ps -Af | grep kdeinit | grep -v grep`
  56. ISGNOME=`ps -Af | grep gnome-panel | grep -v grep`
  57. fi;
  58. KDIALOG=`which kdialog`
  59. ZENITY=`which zenity`
  60. CAPTION=${1}
  61. FILESIZE=${2}
  62. WGETPID=${3}
  63. progresswindow=""
  64. TYPE=""
  65. PIPE="progresspipe_${$}"
  66. retval="1"
  67. CONTINUE="1"
  68. readprogress() {
  69. data=""
  70. input=""
  71. while [ ${CONTINUE} -eq "1" -a -e ${PIPE} ]; do
  72. if [ "${TYPE}" = "KDE" ]; then
  73. if [ `dcop ${progresswindow} wasCancelled` = "true" ]; then
  74. break;
  75. fi
  76. fi;
  77. input=`cat "${PIPE}" | tail ${TAILOPTS}1`
  78. if [ "${input}" = "quit" ]; then
  79. break;
  80. elif [ "${input}" != "" ]; then
  81. data=${input}
  82. input=""
  83. res=`echo "scale=4 ; (${data}/${FILESIZE})*100" | bc`
  84. val=${res%%.*}
  85. if [ "${val}" = "" ]; then
  86. val=0
  87. fi;
  88. if [ "${TYPE}" = "KDE" ]; then
  89. dcop ${progresswindow} setProgress ${val}
  90. if [ ${?} -ne 0 ]; then
  91. break;
  92. fi;
  93. elif [ "${TYPE}" = "GNOME" ]; then
  94. echo ${val}
  95. if [ $? -ne 0 ] ; then
  96. break;
  97. fi
  98. else
  99. if [ $((${val} % 2)) -eq 0 ]; then
  100. echo "-> "${val}"%"
  101. fi;
  102. fi;
  103. if [ "${val}" = "100" ]; then
  104. retval="0"
  105. CONTINUE="0"
  106. break;
  107. fi;
  108. fi;
  109. done;
  110. }
  111. if [ "" = "${CAPTION}" -o "" = ${FILESIZE} ]; then
  112. echo "Insufficient Parameters."
  113. echo "Usage: ${0} <caption> <totalvalue> [pipename]"
  114. exit;
  115. fi;
  116. if [ "" != "${3}" ]; then
  117. # Assume pipe name is what we want, delete existing file.
  118. PIPE=${3}
  119. rm -Rf ${PIPE}
  120. else
  121. # Make sure we get a file that doesn't already exist, keep appending our
  122. # pid untill we get a file that hasn't been taken.
  123. while [ -e "${PIPE}" ]; do
  124. PIPE="${PIPE}_${$}"
  125. done;
  126. fi;
  127. echo "Using pipe: "${PIPE}
  128. mkfifo "${PIPE}"
  129. closeProgress() {
  130. CONTINUE="0"
  131. if [ "${TYPE}" = "KDE" -a ${retval} != "0" ]; then
  132. dcop ${progresswindow} close
  133. fi;
  134. echo "Exiting with value: $retval"
  135. if [ -e ${PIPE} ]; then
  136. echo "Attempting to kill wget"
  137. kill -9 ${WGETPID}
  138. echo "Emptying pipe"
  139. cat ${PIPE};
  140. echo "Deleting Pipe ${PIPE}"
  141. rm -Rf "${PIPE}"
  142. fi;
  143. exit $retval;
  144. }
  145. trap 'closeProgress' INT TERM EXIT
  146. if [ "" != "${ISKDE}" -a "" != "${KDIALOG}" -a "" != "${DISPLAY}" ]; then
  147. echo "Progress dialog on Display: ${DISPLAY}"
  148. progresswindow=`${KDIALOG} --title "DMDirc: ${CAPTION}" --progressbar "${CAPTION}" 100`
  149. dcop ${progresswindow} setAutoClose true
  150. dcop ${progresswindow} showCancelButton true
  151. TYPE="KDE"
  152. readprogress
  153. CONTINUE="0"
  154. echo "Progress Bar Complete"
  155. elif [ "" != "${ISGNOME}" -a "" != "${ZENITY}" -a "" != "${DISPLAY}" ]; then
  156. echo "Progress dialog on Display: ${DISPLAY}"
  157. TYPE="GNOME"
  158. readprogress | ${ZENITY} --progress --auto-close --auto-kill --title "DMDirc: ${CAPTION}" --text "${CAPTION}"
  159. CONTINUE="0"
  160. echo "Progress Bar Complete"
  161. else
  162. echo "Progress For: ${CAPTION}"
  163. echo "-> 0%"
  164. readprogress
  165. CONTINUE="0"
  166. echo ""
  167. echo "Finished!"
  168. fi
  169. echo "Exiting progressbar"
  170. exit 0;