Help in shell script

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?

Mmm.. Not a perfect answere just a try ...
You can run

sudo su

Then you can run that script

And welcome to garuda forum :smile:

i know that i tried

GR=$($DOAS $USER echo $XDG_SESSION_TYPE)

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)

I use different oses at a time.

Arch Linux, Artix Linux, Garuda KDE Lite edition

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. :man_shrugging:
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.

And, welcome of course!

:mage:

BUT, its sound like you want to re-invent the wheel.
Maybe you should take a look at Ansible for creating your system.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.