Helper functions

Friday, July 29th, 2005 - Español English

Some functions providing nice output formatting and easier user input parsing used by some of my scripts. Get them. Or view them…

  1. #!/bin/bash
  2. #
  3. # Wed Aug 17 11:57:21 CEST 2005 * Miguel de Benito <nonick AT 8027 DOT org>
  4. #
  5. # Helper functions for my scripts
  6. #
  7.  
  8. # Some colors
  9. #
  10. BLUE="\033[34;01m"
  11. CYAN="\033[36;01m"
  12. GREEN="\033[32;01m"
  13. RED="\033[31;01m"
  14. OFF="\033[0m"
  15.  
  16.  
  17. #
  18. # Required tools
  19. #
  20.  
  21. [ ! -x /bin/stty ] && error_msg "/bin/stty is required but not found. Bye..."
  22. [ ! -x /bin/date ] && error_msg "/bin/date is required but not found. Bye..."
  23.  
  24.  
  25. #
  26. # Terminal size
  27. #
  28. _ROWS=$(/bin/stty size | cut -d" " -f 1)
  29. _COLS=$(/bin/stty size | cut -d" " -f 2)
  30.  
  31.  
  32. ####
  33. # Outputs info message (no \n)
  34. #
  35. info_msg()
  36. {
  37. echo -ne "$1"
  38. }
  39.  
  40.  
  41. ####
  42. # Outputs status message [OK] [ERR] depending on first parameter
  43. #
  44. status_msg()
  45. {
  46. if (( $1 == 0 ))
  47. then
  48. # Align to the rightmost column
  49. let pos=_COLS-6
  50. echo -e "\033[${pos}G[ ${GREEN}OK${OFF} ]"
  51. else
  52. # Align to the rightmost column
  53. let pos=_COLS-9
  54. echo -e "\033[${pos}G[ ${RED}ERROR${OFF} ]"
  55. fi
  56. }
  57.  
  58. ####
  59. # Outputs colored error messages
  60. #
  61. error_msg()
  62. {
  63. echo -e "${RED}$1${OFF}"
  64. }
  65.  
  66.  
  67. ####
  68. # Reads user input (Y/N)
  69. #
  70. # @arg1 Message
  71. # @arg2 Default option (Y/N)
  72. #
  73. # @return 1 if the user answered no, 0 if yes.
  74. #
  75. ask_msg()
  76. {
  77. if (( $# != 2))
  78. then
  79. error_msg "ask_msg(): wrong number of arguments"
  80. exit 1
  81. fi
  82.  
  83. echo -ne "$1 [${BLUE}$2${OFF}] ${BLUE}>${OFF} "
  84.  
  85. read -s -n 1 inp
  86.  
  87. ## Uh? This looks ugly. Surely it could be improved.
  88. case $2 in
  89. n|N)
  90. case $inp in
  91. y|Y|s|S)
  92. echo -e "${CYAN}Yes${OFF}"
  93. return 0
  94. ;;
  95. *)
  96. echo -e ${BLUE}No${OFF}
  97. return 1
  98. ;;
  99. esac
  100. ;;
  101. y|Y|s|S)
  102. case $inp in
  103. n|N)
  104. echo -e "${CYAN}No${OFF}"
  105. return 1
  106. ;;
  107. *)
  108. echo -e ${BLUE}Yes${OFF}
  109. return 0
  110. ;;
  111. esac
  112. ;;
  113. *)
  114. error_msg "ask_msg(): incorrect default option '$2'"
  115. exit 1
  116. ;;
  117. esac
  118. }
  119.  
  120.  
  121. ##
  122. # Runs a command and shows a status message after completion.
  123. # Logs stderr too.
  124. #
  125. # @arg1 Info message
  126. # @arg2 Command to run
  127. # TODO: better log format, and make filename configurable. Add dates to messages.
  128. run_cmd()
  129. {
  130. local log=/tmp/run_cmd_error.log
  131. local output=""
  132.  
  133. if (( $# != 2))
  134. then
  135. error_msg "run_cmd(): wrong number of arguments"
  136. exit 1
  137. fi
  138.  
  139. date=`/bin/date`
  140.  
  141. info_msg "$1"
  142.  
  143. # Run the command
  144. output=`eval "$2" 2>&1`
  145. status=$?
  146.  
  147. # Check its output
  148. if [ ! -z "$output" ]
  149. then
  150. echo -e "run_cmd(): output of command [$2] at [$date]:\n" >> $log
  151. # Remember to quote $output to preserve linefeeds
  152. echo "$output" >> $log
  153. fi
  154.  
  155. status_msg $status
  156. }
  157.  

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>