最近对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