Hi, Garuda users. I need a little help . I created a shell script for me to automate configuration of new linux installations for my personal usage. But i have the problem of getting display session whether x11 or wayland. for which i use in shell script GR=$(echo XDG_SESSION_TYPE) but this works as non-root user and i need to run my script as root for other tasks also. so how can i deal with it?
GR=$(echo $XDG_SESSION_TYPE)
if [ "$GR" = "x11" ] ; then
CLIP="xclip"
elif [ "$GR" = "wayland" ]
then
CLIP="wl-clipboard"
else
err "Graphical Session not detected."
fi
how to run this as non-root user or is there any other approach to this?
where DOAS is assigned to first recognize whether the system has sudo or doas and if sudo then DOAS = sudo -u and if doas the DOAS= doas -u… and since i have this for my personal setup so USER is assigned USER=$(id -nu 1000) and 1000 uid is of second account. so you get the idea. i will always have 2 users the root uid 0 and my custom user with uid 1000…
for your info id -nu 1000 prints the username of uid 1000.
BUT THIS ALSO FAILS
in general this will be equal to …
GR=$(sudo -u user echo $XDG_SESSION_TYPE)
or
GR=$(doas -u user echo $XDG_SESSION_TYPE)
It seems to be tricky indeed. Environment variables may be set in different places at different times.
I just tried, also passing the -i option to sudo since, at least here (Garuda Sway), XDG_SESSION_TYPE is exported in .profile. No dice. Yet HOME and some other vars are correct for the user.
Also tried to use env in place of echo, it did not work.
Possible workarounds (not tried):
1- if you know where it is exported from, you could grep the relevant file (maybe it's always .profile)
2- have a first step run as the normal user which writes the value in a known location before running the main script as root. Ugly I know but no one else will see it, right?
Then again, I'm no expert and I'll probably facepalm very soon LOL.