Skip to content

Instantly share code, notes, and snippets.

@GluTbl
Last active March 29, 2021 08:03
Show Gist options
  • Save GluTbl/8a36a65ab852d4ed782bd87f9e5f1a74 to your computer and use it in GitHub Desktop.
Save GluTbl/8a36a65ab852d4ed782bd87f9e5f1a74 to your computer and use it in GitHub Desktop.
[notify in linux from sudo or anything ] #linux #shell
#!/bin/bash
POSITIONAL=()
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-t|--time)
TIME="$2"
shift # past argument
shift # past value
;;
-i|--icon)
ICON="$2"
shift # past argument
shift # past value
;;
*) # unknown option
POSITIONAL+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters
#Detect the name of the display in use
display=":$(ls /tmp/.X11-unix/* | sed 's#/tmp/.X11-unix/X##' | head -n 1)"
#Detect the user using such display
user=$(who | grep '('$display')' | awk '{print $1}' | head -n 1)
#Detect the id of the user
uid=$(id -u $user)
if [ -z "$TIME" ]
then
if [ -z "$ICON" ]
then
sudo -u $user DISPLAY=$display DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$uid/bus notify-send "$1" "$2"
else
sudo -u $user DISPLAY=$display DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$uid/bus notify-send "$1" "$2" -t "$TIME"
fi
else
if [ -z "$ICON" ]
then
sudo -u $user DISPLAY=$display DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$uid/bus notify-send "$1" "$2" -i "$ICON"
else
sudo -u $user DISPLAY=$display DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$uid/bus notify-send "$1" "$2" -t "$TIME" -i "$ICON"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment