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
Per tentative's suggestion this is the solution: