Advertisement
Guest User

print_brithdays.sh

a guest
Apr 13th, 2014
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.86 KB | None | 0 0
  1. #!/bin/bash
  2. ########################################################################
  3. #    Author: Henning Hollermann, laclaro@mail.com                      #
  4. #                                                                      #
  5. #    This program is free software: you can redistribute it and/or     #
  6. #    modify it under the terms of the GNU General Public License       #
  7. #    as published by the Free Software Foundation, either version 3    #
  8. #    of the License, or (at your option) any later version             #
  9. #                                                                      #
  10. #    This program is distributed in the hope that it will be useful,   #
  11. #    but WITHOUT ANY WARRANTY; without even the implied warranty of    #
  12. #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the      #
  13. #    GNU General Public License for more details.                      #
  14. #                                                                      #
  15. #    You should have received a copy of the GNU General Public License #
  16. #    along with this program. If not, see <http://gnu.org/licenses/>.  #
  17. #                                                                      #
  18. #    Bash-Script read list of birthday dates and names with lines      #
  19. #    "dd.mm.yyyy NAME" and prints birthdays that are nearby as con-    #
  20. #    figured.                                                          #
  21. #                                                                      #
  22. #    Written in simple bash and awk                      .             #
  23. #                                                                      #
  24. ########################################################################
  25.  
  26. # SETTINGS
  27. birthday_list=$HOME/.geburtstagsliste
  28. TOLERANCE=5
  29. DAY="$(date +%d)"
  30. MONTH="$(date +%m)"
  31. YEAR="$(date +%Y)"
  32. # Sort file every 6 hours
  33. SORT=6
  34.  
  35. # START SCRIPT
  36. if [ ! -r $birthday_list ]; then
  37.     echo "Error: file $birthday_list not found or not readable." 1>&2
  38.     exit 1
  39. fi
  40.  
  41. # sort list (so you can easily add birthdays, right at the bottom)
  42. if [ $(find $birthday_list -mmin +$(($SORT*60)) -type f 2>/dev/null) ]; then
  43.     sort -n +1 -2 -t'.' $birthday_list -o $birthday_list
  44. fi
  45.  
  46. awk '
  47.    BEGIN {FS="[.]+|[ \t]+"; N=0; };
  48.    {if ( $2==('$MONTH') && $1<=('$DAY'+'$TOLERANCE') && $1>=('$DAY'-'$TOLERANCE') ) {
  49.        day=$1; month=$2; year=$3; $1=$2=$3=""; gsub ("^[ \t]+", "", $0); name=$0;
  50.        monthnamecommand="date --date="month"/"day" +%B";
  51.        monthnamecommand | getline monthname;
  52.        message = day". " monthname" "name;
  53.        # if recent birthdays are present, add age
  54.        if ('$YEAR'-year!='$YEAR') message = message " ("'$YEAR'-year")";
  55.        # mark todays birthdays
  56.        if (day == '$DAY') message="> "message;
  57.        print message;
  58.        N = N+1;
  59.    }}
  60.    END { if (N==0) print "Keine zeitnahen Geburtstage"}
  61. ' $birthday_list
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement