angrykoala
angrykoala

Reputation: 4054

Execute pkexec command on a different path

I'm writing a graphical app that will prompt a pkexec window at one point, however, using pkexec will change the path of execution to /root dir, I'm trying to execute the command in the previous path. For example

pwd # returns /home/myuser/Desktop
pkexec pwd # returns /root

I would like the second pkexec to execute pwd in /home/myuser/Desktop. cd won't work with pkexec and I see no option to execute or go to a different path

Upvotes: 4

Views: 1051

Answers (1)

Patrick B.
Patrick B.

Reputation: 484

pkexec appears not to do this by design. I had a really specific use case where I wanted pkexec to be able to execute things in the working directory. The answer I came up with was to write two scripts: script A changes directory to the first argument and then executes the other arguments as a command, and script B calls pkexec on script A, with the current directory as the first argument and the rest of the arguments as the "real" arguments.

Script A ("exec-in-dir" -- must be installed in /usr/local/bin or somewhere universal):

#!/bin/bash

cd $1
shift
eval $@

Script B ("pkw"):

#!/bin/bash

pkexec exec-in-dir $PWD "$@"

Kind of a hack, but it seems to work.

Note that by doing this you're kind of subverting some design decisions made when creating pkexec, for better or for worse. I would be careful about using this wrapper for graphical applications in particular, since part of the reason pkexec always changes directories is to avoid, for example, nautilus starting as root in your user directory and messing up all your permissions.

Upvotes: 3

Related Questions