Advertisement
LanceCaraccioli

Desktop Notifications 4 Sys. Scripts (cron, init.d, upstart)

Nov 22nd, 2013
1,330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.99 KB | None | 0 0
  1. #!/bin/bash
  2. me=$0;
  3. usage(){
  4.     echo
  5.     echo 'Overview:'
  6.     echo '- Must be run as super user (i.e. root).'
  7.     echo '- Delegates to the notify-send command. See notify-send man page for additional options.'
  8.     echo '- <username> is the username of the user who will receive the desktop notification.'
  9.     echo
  10.     echo "Usage:"
  11.     echo "- \"sudo notify-send-user -U <username> [notify-send options]\" - General pattern send <username> a desktop notification. See notify-send man page."
  12.     echo "- \"sudo notify-send-user -U johndoe -i info \"Subject\" \"Message loreum ipsum dolor\"\" - Mock usage for hypothetical user named johndoe."
  13.     echo "- \"notify-send-user -h\" - display usage information (what is displayed currently)"
  14.     echo
  15. }
  16.  
  17.  
  18. #Process command line to retrieve the user who will receive the notification
  19. while getopts ":hU:" option; do
  20.     case "$option" in
  21.         h) usage
  22.             exit 0;;
  23.         U) user=$OPTARG;;
  24.     :) echo "  Error: -$OPTARG requires an argument."
  25.         usage
  26.             exit 0;;
  27.         ?) ;; #unknown option - path thru to notify-send
  28.         *) ;; #unimplimented option - path thru to notify-send
  29.     esac
  30. done
  31.  
  32. #check that a username was provided
  33. if [ -z "$user" ]
  34. then
  35.     echo "  Error: a valid username must be specified"
  36.     usage
  37.     exit 0
  38. fi
  39.  
  40. export DISPLAY=:0.0
  41. #build the wrapped notify-send by passing all command line arguments except the -U options which is specific to this script
  42.  
  43. command="sudo -u $user notify-send";
  44. for input in "${@:1}"; do
  45.     if [[ "$input" = "$user" ||  "$input" = "-U" ]]
  46.     then
  47.         command+='' #skip
  48.     else
  49.     command+=$IFS #input field separator
  50.  
  51.         #input contains spaces so add single quotes (expansion has already occured) and escape single quotes
  52.    
  53.     if [[ "$input" =~ " " ]]; then
  54.         escaped=${input/[']/\'/g}
  55.         command+="'"
  56.         command+="${escaped}"
  57.         command+="'"
  58.     else
  59.         command+="$input"
  60.     fi;
  61.     fi;
  62. done;
  63.  
  64. #run the rebuilt notify-send command
  65. eval $command
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement