Advertisement
MestreLion

lib/common - library of common functions used in my personal

Jul 30th, 2011
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.91 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # lib/common - library of common functions used in my personal scripts
  4. #
  5. #    Copyright (C) 2011 Rodrigo Silva - <[email protected]>
  6. #
  7. #    This program is free software: you can redistribute it and/or modify
  8. #    it under the terms of the GNU General Public License as published by
  9. #    the Free Software Foundation, either version 3 of the License, or
  10. #    (at your option) any later version.
  11. #
  12. #    This program is distributed in the hope that it will be useful,
  13. #    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. #    GNU General Public License for more details.
  16. #
  17. #    You should have received a copy of the GNU General Public License
  18. #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  19.  
  20.  
  21. exec 3> /dev/null # to avoid harcoding /dev/null everywhere. For tools' stderr.
  22.  
  23. SELF="${0##*/}" # buitin $(basename $0)
  24.  
  25.  
  26. # _TestRequirements() { #TODO }
  27.  
  28. testroot()
  29. {
  30.     # simple root test
  31.     [[ "$(id -u)" -eq 0 ]] || fatal "You must run this as root"
  32. }
  33.  
  34. confirm()
  35. {
  36.     # For both default parameter and user input,
  37.     # non-empty garbage will always evaluate to and behave as NO
  38.     local message="$1"
  39.     local default="${2:-y}"
  40.     local silentno="$3"
  41.    
  42.     local question="NO" ; [[ "$default" = "y" ]] && question="YES"
  43.    
  44.     if [ -z "$FORCE" ] ; then
  45.  
  46.         read -p "$message (y/n, default $question): " confirm
  47.         confirm="${confirm:-$default}"
  48.        
  49.         case "$confirm" in
  50.         [Yy]*) ;;
  51.         *    ) [[ "$VERBOSE" && -z "$silentno" ]] && \
  52.                  echo "$SELF: ${COMMAND:+$COMMAND: }cancelled by user"
  53.                return 2 ;;
  54.         esac
  55.  
  56.     fi
  57.    
  58.     return 0
  59. }
  60.  
  61. fatal()
  62. {
  63.     local message="$1"
  64.     local errorcode="${2:-1}"
  65.     local dump="$3"
  66.    
  67.     [[ -n "$dump"    ]] && echo "$dump" >&2
  68.     [[ -n "$message" ]] && echo "$SELF: ${COMMAND:+$COMMAND: }${message/%[[:punct:]]/}" >&2
  69.  
  70.     exit $errorcode
  71. }
  72.  
  73. fatalgui()
  74. {
  75.     local message="$1"
  76.     local errorcode="${2:-1}"
  77.     local dump="$3"
  78.    
  79.     zenity --error --title="$SELF_GUI" \
  80.            --text="${message:+$message\n\n}${dump:+$dump\n\n}"
  81.                  #"Exiting with error code ${errorcode}"
  82.    
  83.     fatal "$message" "$errorcode" "$dump"
  84. }
  85.  
  86. min() { (( $1 < $2 )) && printf %d "$1" || printf %d "$2"; }
  87. max() { (( $1 > $2 )) && printf %d "$1" || printf %d "$2"; }
  88.  
  89. debug()
  90. {
  91.     local var="$1"
  92.     declare -p var
  93.     exit
  94. }
  95.  
  96. xdg-data-list()
  97. {
  98.     local path="$1"
  99.     local user
  100.     local system
  101.     local folder
  102.     local list=()
  103.  
  104.     IFS=: read -ra system <<< "${XDG_DATA_DIRS:-/usr/local/share/:/usr/share/}"
  105.    
  106.     for folder in "${system[@]}" ; do
  107.         if [[ -e "$folder/$path" ]] ; then
  108.             list+=("${folder%/}/$path")
  109.         fi
  110.     done
  111.    
  112.     user="${XDG_DATA_HOME:-$HOME/.local/share}/$path"
  113.     [[ -e "$user" ]] && list+=("$user")
  114.    
  115.     xdg_data_list_RESULT=("${list[@]}")
  116. }
  117.  
  118. xdg-data-cat()
  119. {
  120.     local path="$1"
  121.    
  122.     xdg-data-list "$path"
  123.     cat "${xdg_data_list_RESULT[@]}"
  124.     unset xdg_data_list_RESULT
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement