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 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. ISKDE=`pidof -x -s kdeinit`
  13. KDIALOG=`which kdialog`
  14. ISGNOME=`pidof -x -s gnome-panel`
  15. ZENITY=`which zenity`
  16. CAPTION=${1}
  17. FILESIZE=${2}
  18. progresswindow=""
  19. TYPE=""
  20. PIPE="progresspipe_${$}"
  21. retval="1"
  22. readprogress() {
  23. data=""
  24. input=""
  25. while [ -e ${PIPE} ]; do
  26. if [ "${TYPE}" = "KDE" ]; then
  27. if [ `dcop ${progresswindow} wasCancelled` = "true" ]; then
  28. break;
  29. fi
  30. fi;
  31. input=`cat "${PIPE}"`
  32. if [ "${input}" == "quit" ]; then
  33. break;
  34. elif [ "${input}" != "" ]; then
  35. data=${input}
  36. input=""
  37. res=`echo "scale=4 ; (${data}/${FILESIZE})*100" | bc`
  38. val=${res%%.*}
  39. if [ "${val}" = "" ]; then
  40. val=0
  41. fi;
  42. if [ "${TYPE}" = "KDE" ]; then
  43. dcop ${progresswindow} setProgress ${val}
  44. if [ ${?} -ne 0 ]; then
  45. break;
  46. fi;
  47. elif [ "${TYPE}" = "GNOME" ]; then
  48. echo ${val}
  49. if [ $? -ne 0 ] ; then
  50. break;
  51. fi
  52. else
  53. if [ $((${val} % 2)) -eq 0 ]; then
  54. echo "-> "${val}"%"
  55. fi;
  56. fi;
  57. if [ "${val}" = "100" ]; then
  58. retval="0"
  59. break;
  60. fi;
  61. fi;
  62. done;
  63. }
  64. if [ "" = "${CAPTION}" -o "" = ${FILESIZE} ]; then
  65. echo "Insufficient Parameters."
  66. echo "Usage: ${0} <caption> <totalvalue> [pipename]"
  67. exit;
  68. fi;
  69. if [ "" != "${3}" ]; then
  70. # Assume pipe name is what we want, delete existing file.
  71. PIPE=${3}
  72. rm -Rf ${PIPE}
  73. else
  74. # Make sure we get a file that doesn't already exist, keep appending our
  75. # pid untill we get a file that hasn't been taken.
  76. while [ -e "${PIPE}" ]; do
  77. PIPE="${PIPE}_${$}"
  78. done;
  79. fi;
  80. echo "Using pipe: "${PIPE}
  81. mkfifo "${PIPE}"
  82. closeProgress() {
  83. if [ "${TYPE}" = "KDE" -a ${retval} != "0" ]; then
  84. dcop ${progresswindow} close
  85. fi;
  86. echo "Deleting ${PIPE}"
  87. rm -Rf "${PIPE}"
  88. exit $retval;
  89. }
  90. trap 'closeProgress' INT TERM EXIT
  91. if [ "" != "${ISKDE}" -a "" != "${KDIALOG}" -a "" != "${DISPLAY}" ]; then
  92. echo "Progress dialog on Display: ${DISPLAY}"
  93. progresswindow=`${KDIALOG} --title "DMDirc: ${CAPTION}" --progressbar "${CAPTION}" 100`
  94. dcop ${progresswindow} setAutoClose true
  95. dcop ${progresswindow} showCancelButton true
  96. TYPE="KDE"
  97. readprogress
  98. elif [ "" != "${ISGNOME}" -a "" != "${ZENITY}" -a "" != "${DISPLAY}" ]; then
  99. echo "Progress dialog on Display: ${DISPLAY}"
  100. TYPE="GNOME"
  101. readprogress | ${ZENITY} --progress --auto-close --auto-kill --title "DMDirc: ${CAPTION}" --text "${CAPTION}"
  102. else
  103. echo "Progress For: ${CAPTION}"
  104. echo "-> 0%"
  105. readprogress
  106. echo ""
  107. echo "Finished!"
  108. fi