How to get pkexec's return value?

224 views Asked by At

pkexec's return value is 126 when the user doesn't authenticate. How can I use it? I tried assigning pkexec to a variable for later use, but that doesn't work. Else, how can I set auth to 1 when pkexec is run and the user authenticates, so it's usable later on in the script?

I could use sudo instead to spare me the trouble, (the script will chmod some files in /usr/bin/) but I need the script to be user friendly, it will be called from the start menu, so I prefer pkexec's GUI.

#!/bin/bash

auth=0

pkexec bash -c "[do something as root];auth=1"

if [ $auth = 1 ]; then
    [do something]
elif [ $auth = 0 ]; then
    [do something else]
fi

# prints 0, not 1, even if the user has authenticated and run pkexec
echo $auth
1

There are 1 answers

0
Marek Möhling On

Per tentative's suggestion this is the solution:

pkexec bash -c "chmod 000 /usr/bin/mono"

auth=$?

if [ $auth = 0 ]; then
    echo ok
elif [ $auth = 126 ]; then
    echo sorry
fi