pkexec 和 x-terminal-emulator

最近对timeshift(一个系统备份还原的工具), loongnix的系统3年前发布的第一个版本,就预留了一块分区,用于系统备份,但一直都没用起来,现在对开源的timeshift进行改进:

启动timeshift时,自动挂载;关闭时,自动卸载;设置默认备份路径。当然,这不是我今天写这篇博客的原因。

我觉着有段代码有两个命令对初学者有些帮助,pkexec 和 x-terminal-emulator:

$ cat  src/timeshift-launcher 
#!/bin/bash

app_command='timeshift-gtk'
#app_command='timeshift-mount'

if [ `id -u` -eq 0 ]; then
	#user is admin
	${app_command}
else
	#user is not admin
	if `echo $- | grep "i" >/dev/null 2>&1`; then
		#script is running in interactive mode
		su - -c "${app_command}"
	else
		#script is running in non-interactive mode
		if [ $XDG_SESSION_TYPE = "wayland" ] ; then
			xhost +SI:localuser:root
			pkexec ${app_command}
			xhost -SI:localuser:root
			xhost
		elif command -v pkexec >/dev/null 2>&1; then
			pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY ${app_command}
		elif command -v sudo >/dev/null 2>&1; then
			x-terminal-emulator -e "sudo ${app_command}"
		elif command -v su >/dev/null 2>&1; then
			x-terminal-emulator -e "su - -c '${app_command}'"
		else
			x-terminal-emulator -e "echo 'Command must be run as root user: ${app_command}'"
		fi
	fi
fi

此处有两个知识点:

一是,x-terminal-emulator和pkexec是在shell脚本中用other user执行常用的命令,可弹出一个终端或者认证窗口.若没指定username,默认是root

二是,两个与显示或登录时相关的两个环境变量,若想从登录界面进入系统,不可缺少的两个环境变量:

$DISPLAY

$XAUTHORITY

本篇不展开讲,有兴趣的同学可以自学一下,后续有时间会继续更新,Bye

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值