Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- me=$0;
- usage(){
- echo
- echo 'Overview:'
- echo '- Must be run as super user (i.e. root).'
- echo '- Delegates to the notify-send command. See notify-send man page for additional options.'
- echo '- <username> is the username of the user who will receive the desktop notification.'
- echo
- echo "Usage:"
- echo "- \"sudo notify-send-user -U <username> [notify-send options]\" - General pattern send <username> a desktop notification. See notify-send man page."
- echo "- \"sudo notify-send-user -U johndoe -i info \"Subject\" \"Message loreum ipsum dolor\"\" - Mock usage for hypothetical user named johndoe."
- echo "- \"notify-send-user -h\" - display usage information (what is displayed currently)"
- echo
- }
- #Process command line to retrieve the user who will receive the notification
- while getopts ":hU:" option; do
- case "$option" in
- h) usage
- exit 0;;
- U) user=$OPTARG;;
- :) echo " Error: -$OPTARG requires an argument."
- usage
- exit 0;;
- ?) ;; #unknown option - path thru to notify-send
- *) ;; #unimplimented option - path thru to notify-send
- esac
- done
- #check that a username was provided
- if [ -z "$user" ]
- then
- echo " Error: a valid username must be specified"
- usage
- exit 0
- fi
- export DISPLAY=:0.0
- #build the wrapped notify-send by passing all command line arguments except the -U options which is specific to this script
- command="sudo -u $user notify-send";
- for input in "${@:1}"; do
- if [[ "$input" = "$user" || "$input" = "-U" ]]
- then
- command+='' #skip
- else
- command+=$IFS #input field separator
- #input contains spaces so add single quotes (expansion has already occured) and escape single quotes
- if [[ "$input" =~ " " ]]; then
- escaped=${input/[']/\'/g}
- command+="'"
- command+="${escaped}"
- command+="'"
- else
- command+="$input"
- fi;
- fi;
- done;
- #run the rebuilt notify-send command
- eval $command
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement