• How to open a file as normal user using pkexec?


    Hi!
    Yesterday i was trying to learn a little more about pkexec since gksu now is going down for real.

    I found a problem though:
    I tried to open an already existent file with pkexec MY_TEXT_EDITOR /path/to/file but a new file with the same name is created for root user.
    I also attempted to open with pkexec --user MYUSER MY_TEXT_EDITOR /path/to/file in order to open the file under my user, this last command didn’t work.

    So any ideas how could i use pkexec to my user, or it was not created for that purpose?

  • @joekamprad said in How to open a file as normal user using pkexec?:

    @fernandomaroto said in How to open a file as normal user using pkexec?:

    pkexec

    https://bbs.archlinux.org/viewtopic.php?pid=999328#p999328

    I have a file config already.
    It seems that only full paths work with pkexec. So we can’t simply cd to a directory and pkexec EDITOR file or pkexec EDITOR ./file. Needs to be pkexec EDITOR /path/file. It accepts abreviations like ~ though…

  • @fernandomaroto
    Maybe use this for files in the current folder:

    cd <somewhere-you-want>
    pkexec <your-editor> $PWD/<file-in-this-folder>
    

    :)

  • Or make a simplistic (=very limited) script into ~/.bashrc:

    pkexec()
    {
        # supports only this syntax: pkexec <command> <file>
        local command="$1"
        local file="$2"
        if [ "${file::1}" != "/" ] && [ -f "$file" ] ; then
            file="$PWD/$file"
        fi
        command pkexec "$command" "$file"
    }
    

    Then give command
    source ~/.bashrc
    and enjoy your new pkexec command!

  • @manuel said in How to open a file as normal user using pkexec?:

    Or make a simplistic (=very limited) script into ~/.bashrc:

    pkexec()
    {
        # supports only this syntax: pkexec <command> <file>
        local command="$1"
        local file="$2"
        if [ "${file::1}" != "/" ] && [ -f "$file" ] ; then
            file="$PWD/$file"
        fi
        command pkexec "$command" "$file"
    }
    

    Then give command
    source ~/.bashrc
    and enjoy your new pkexec command!

    Ah, very nice, thanks. I was trying to create an alias to make things easier, now i just need to understand your script hehehe (not so simplistic as you said).
    Thank you again!

  • @fernandomaroto
    An alias is much more limited than a function, aliases cannot solve more than only the simplest cases.

    But if you “crack” that pkexec function, then you know quite a lot about bash! ;)

    If you need any explanation, just let me know. :)

pkexec4 normal3 Posts 7Views 334
Bloom Email Optin Plugin

Looks like your connection to Antergos Community Forum was lost, please wait while we try to reconnect.