0

I'm writing a script to copy my display settings from my user to root, to avoid the X11 problem when running GUIs as sudo (ex. vivado install on remote machine)

the thing is that this works:

sudo xauth add $(xauth -f ~theuser/.Xauthority list|tail -1)

and this:

xauth -f ~theuser/.Xauthority list|tail -1

has the output:

servername/unix:12  MIT-MAGIC-COOKIE-1  1ec6eb59738a492928b63451d2c63304

But, and here is my problem, I want to create a script so this can be done by any user

x11sudo.sh

currentUser=$(whoami)
sudo xauth add $(xauth -f ~$currentUser/.Xauthority list|tail -1)

but doesn't work.

The issue is in xauth -f ~$currentUser/.Xauthority list|tail -1 not recognizing the path to .Xauthority

if in the command line I write:

theuser@servername:~ $ currentUser=$(whoami)
theuser@servername:~ $ echo $currentUser
theuser
theuser@servername:~ $ xauth -f ~$currentUser/.Xauthority list|tail -1
xauth:  error in locking authority file ~theuser/.Xauthority
theuser@servername:~ $

I've tried some iterations, adding echo, "", '' and others but always the same result, it doesn't like the text string ~theuser/.Xauthority if it's created with a variable.

Thank you very much

taquionbcn
  • 543
  • 1
  • 8
  • 25
  • 1
    But, why not just `~/.Xauthority`? `doesn't work.` What happens? `not recognizing the path to .Xauthority` How do you know? Is there any error message? – KamilCuk Jan 11 '22 at 16:21
  • 2
    `~` processing happens *before* variable expansion. So you can't use `~$theuser` to get the home directory of `$theuser`. – Barmar Jan 11 '22 at 16:29
  • Why do you think you need a variable for this? The commands inside `$(...)` are executed in your current shell, not the root shell of `sudo`. – Barmar Jan 11 '22 at 16:30
  • Thank you both(@Barmar & @KamilCuk)! that was the answer and the knowledge of why is not working. – taquionbcn Jan 11 '22 at 16:35
  • BTW: Add an `echo` to your commandline, e.g. `echo xauth -f ~$currentUser/...` to find out what actual commandline the shell executes. Also, reduce your problem: You'd find out that this doesn't have anything to do with x11, centos or xauth, it's just bash. The "linux" tag is completely wrong, too, see its description. – Ulrich Eckhardt Jan 11 '22 at 16:44

1 Answers1

0

Solved and very fast.

As @Kamilcuk and @Barmar answered in the comments, my error was to believe that I had to store the user to use it inside the sudo instruction.

I was not aware that $(...) was executed in my user.

then I only have 1 more question related to the ~ expansion. If instead of getting the current user I passed the user from a variable(argument) how would be the syntax?

Thank you!

taquionbcn
  • 543
  • 1
  • 8
  • 25
  • "One more question" doesn't go in an answer -- but I closed the question with a linked duplicate that tells you how you _would_ handle this if you did in fact need to. (Note that the answers range between "not pretty" and "severely insecure"; anything that uses `eval` without also using `printf %q` needs to be treated with extreme skepticism). – Charles Duffy Jan 11 '22 at 16:58