This is a request for improvement and update (NOT a duplicate!) on these 2 questions & answers:
I'm currently on Ubuntu 15.04 and the command who
gives an output like this when run in a root terminal, as my script will be used as a cron job:
sadi :0 2015-10-07 17:32 (:0)
root pts/0 2015-10-07 19:55 (:0)
After studying the questions & answers mentioned above, I wrote these (a little updated, but unfinished) lines:
#!/bin/bash
xuser=${who | grep -m1 ^.*\) | awk '{print $1}'}
xdisplay=${who | grep -m1 ^.*\) | awk '{print $5}' | sed 's/[(|)]//g'}
DISPLAY="$xdisplay" XAUTHORITY=/home/"$xuser"/.Xauthority su "$xuser" -c "notify-send 'Message'"
I wonder why these 2 variables don't work (giving bad substitution
)... Also, in order to make this script applicable to different (e.g. multi-user) configurations, I think I should take into account that there might be other users as well. So I guess I then need to create a loop that will input each user and their X display to the last command, but as a beginner in bash scripting I don't know how...
Until I find an answer to this question I'm using a single-user script like this:
cuser="${SUDO_USER:-$USER}"
...
DISPLAY=:0 XAUTHORITY=/home/"$cuser"/.Xauthority su "$cuser" -c "notify-send 'Message'"
It seems the grep
option -m1
should also be modified (as well as replacing ${...}
with $(...)
) as it merely defines how many lines should be captured from the top of the output.
The command who
- run in a root terminal again - gives an output like this when there is also a guest session, i.e. multi-user environment:
sadi :0 2015-10-08 10:40 (:0)
guest-bAMpqo :1 2015-10-08 12:22 (:1)
root pts/28 2015-10-08 12:43 (:0)
guest-bAMpqo pts/18 2015-10-08 12:25 (:1)
So this should give me a list of names of X users and X Displays:
#!/bin/bash
xusers=$(who | grep ' :[0-9]' | awk '{print $1}')
xdisplays=$(who | grep ' :[0-9]' | awk '{print $5}' | sed 's/[(|)]//g')
DISPLAY="$xdisplay" XAUTHORITY=/home/"$xuser"/.Xauthority su "$xuser" -c "notify-send 'Message'"
Now I need to modify the last command to capture that list line by line.
$(...)
instead of${...}
in your first 2 lines. Both exist in bash. However , they do very different things. Bash should actually tell youbad substitution
.${...}
with$(...)
All I need now to make this work for all X users and their X displays.ksh
. The${
command sub;}
form is specific toksh93
.