1
0

dialog.sh 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #!/usr/bin/env sh
  2. # Usage:
  3. # dailog.sh [tui|gui] [--infobox|--messagebox|--yesno|--gauge] title message
  4. # WARNING: no error checking is done
  5. fake_dialog () {
  6. case "$2" in
  7. --infobox)
  8. echo ""
  9. echo "$3"
  10. echo "$4"
  11. echo ""
  12. ;;
  13. --msgbox)
  14. echo ""
  15. echo "$3"
  16. echo "$4"
  17. read -p "Press [Enter] key to continue ... \$ " DIALOG_RESULT
  18. echo ""
  19. ;;
  20. --yesno)
  21. while true ; do
  22. echo ""
  23. echo "$3"
  24. echo "$4"
  25. read -p "([yes], no) \$ " DIALOG_RESULT
  26. echo ""
  27. case "x$DIALOG_RESULT" in
  28. xno)
  29. DIALOG_RESULT="n"
  30. break
  31. ;;
  32. xNO)
  33. DIALOG_RESULT="n"
  34. break
  35. ;;
  36. xyes)
  37. DIALOG_RESULT="y"
  38. break
  39. ;;
  40. xYES)
  41. DIALOG_RESULT="y"
  42. break
  43. ;;
  44. x)
  45. DIALOG_RESULT="y"
  46. break
  47. ;;
  48. *)
  49. ;;
  50. esac
  51. done
  52. case "$DIALOG_RESULT" in
  53. n)
  54. (exit 1)
  55. ;;
  56. y)
  57. (exit 0)
  58. ;;
  59. *)
  60. (exit 0)
  61. ;;
  62. esac
  63. ;;
  64. --textbox)
  65. echo ""
  66. echo "$3"
  67. if command -v "less" 2>/dev/null 1>/dev/null ; then
  68. less "$4"
  69. else
  70. if command -v "more" 2>/dev/null 1>/dev/null ; then
  71. more "$4"
  72. else
  73. cat "$4"
  74. read -p "Press [Enter] key to continue ... \$ " DIALOG_RESULT
  75. fi
  76. fi
  77. echo ""
  78. ;;
  79. *)
  80. echo "$4"
  81. ;;
  82. esac
  83. }
  84. fake_progress () {
  85. echo ""
  86. echo "$3"
  87. echo "$4"
  88. echo -n "0%..."
  89. while IFS='' read -r line ; do
  90. if [ '(' "$line" -gt 1 ')' -a '(' "$line" -lt 100 ')' ]; then
  91. echo -n "$line%..."
  92. fi
  93. done
  94. echo -n "100%"
  95. echo ""
  96. echo ""
  97. }
  98. if [ "$2" = "--gauge" ]; then
  99. if [ "$1" = "tui" ]; then
  100. DIALOG_LIST="dialog whiptail gdialog xdialog fake_progress"
  101. else
  102. DIALOG_LIST="zenity gdialog xdialog dialog whiptail fake_progress"
  103. fi
  104. for d in $DIALOG_LIST ; do
  105. if [ "$d" = "fake_progress" ]; then
  106. fake_progress "tui" "$2" "$3" "$4"
  107. exit $?
  108. else
  109. if command -v "$d" 2>/dev/null 1>/dev/null ; then
  110. if [ "$d" = "zenity" ]; then
  111. exec $d --title "$3" --auto-close --progress "--text=$4" 0 0 0
  112. else
  113. exec $d --title "$3" "$2" "$4" 0 0 0
  114. fi
  115. fi
  116. fi
  117. done
  118. else
  119. if [ "$1" = "tui" ]; then
  120. DIALOG_LIST="dialog whiptail gdialog xdialog fake_dialog"
  121. else
  122. DIALOG_LIST="gdialog xdialog dialog whiptail fake_dialog"
  123. fi
  124. for d in $DIALOG_LIST ; do
  125. if [ "$d" = "fake_dialog" ]; then
  126. fake_dialog "tui" "$2" "$3" "$4"
  127. exit $?
  128. else
  129. if command -v "$d" 2>/dev/null 1>/dev/null ; then
  130. exec $d --title "$3" "$2" "$4" 0 0
  131. fi
  132. fi
  133. done
  134. fi