如何获取pkexec的返回值?

时间:2021-01-25 08:04:47

标签: bash authentication policy-based-security

当用户未通过身份验证时,pkexec 的返回值为 126。我怎样才能使用它?我尝试将 pkexec 分配给一个变量以供以后使用,但这不起作用。否则,如何在 pkexec 运行且用户进行身份验证时将 auth 设置为 1,以便稍后在脚本中使用?

我可以使用 sudo 来避免麻烦,(脚本将修改 /usr/bin/ 中的一些文件)但我需要脚本对用户友好,它将从开始菜单中调用,所以我更喜欢pkexec 的 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 个答案:

答案 0 :(得分:0)

根据暂定的建议,这是解决方案:

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

auth=$?

if [ $auth = 0 ]; then
    echo ok
elif [ $auth = 126 ]; then
    echo sorry
fi
相关问题