Add 修改 start.sh 中 display_found 变量的赋值方式

This commit is contained in:
Tian jianyong 2025-12-11 15:20:16 +08:00
parent 6896dd9593
commit 0b96a093a6

View File

@ -23,52 +23,44 @@ cd /home/unitree/robot_face_rec || {
echo "Env before display detection: DISPLAY=$DISPLAY, XAUTHORITY=$XAUTHORITY" echo "Env before display detection: DISPLAY=$DISPLAY, XAUTHORITY=$XAUTHORITY"
} >> "$LOG_FILE" } >> "$LOG_FILE"
# --- DISPLAY 设置逻辑 --- # --- DISPLAY 和 XAUTHORITY 设置逻辑 ---
# 使用默认DISPLAY值:0这是Linux系统中最常见的显示编号
display_found=":0"
echo "Using default DISPLAY: :0"
for pid in $(pgrep Xorg); do # 设置DISPLAY环境变量
if [ -r /proc/$pid/environ ]; then # 使用Linux系统最常见的默认值:0
disp=$(tr '\0' '\n' < /proc/$pid/environ | grep '^DISPLAY=' | cut -d'=' -f2-) export DISPLAY=":0"
if [ -n "$disp" ]; then echo "Using DISPLAY: $DISPLAY"
display_found="$disp"
echo "Chosen DISPLAY from Xorg process: $disp"
break
fi
fi
done
# 如果还没找到 DISPLAY则尝试 fallback # 设置XAUTHORITY环境变量
if [ -z "$display_found" ]; then # 先尝试从Xorg进程命令行获取
# 常见默认::0
display_found=":0"
echo "Chosen default DISPLAY: :0"
fi
export DISPLAY="$display_found"
# 从Xorg命令行参数中获取正确的XAUTHORITY路径
xauth_path="" xauth_path=""
for pid in $(pgrep Xorg); do for pid in $(pgrep Xorg); do
if [ -r /proc/$pid/cmdline ]; then if [ -r /proc/$pid/cmdline ]; then
cmdline=$(tr '\0' ' ' < /proc/$pid/cmdline) cmdline=$(tr '\0' ' ' < /proc/$pid/cmdline)
xauth_path=$(echo "$cmdline" | grep -o "-auth [^ ]*" | cut -d' ' -f2) # 使用更兼容的方式提取auth参数
xauth_path=$(echo "$cmdline" | awk '/-auth/{print $2}' | head -1)
if [ -n "$xauth_path" ]; then if [ -n "$xauth_path" ]; then
break break
fi fi
fi fi
done done
# 如果没有找到,使用默认路径 # 如果没有找到,检查常见的XAUTHORITY路径
if [ -z "$xauth_path" ]; then if [ -z "$xauth_path" ]; then
xauth_path="/home/unitree/.Xauthority" # 检查gdm路径
# 尝试更常见的gdm路径
if [ -f "/run/user/1000/gdm/Xauthority" ]; then if [ -f "/run/user/1000/gdm/Xauthority" ]; then
xauth_path="/run/user/1000/gdm/Xauthority" xauth_path="/run/user/1000/gdm/Xauthority"
# 检查lightdm路径
elif [ -f "/var/run/lightdm/root/:0" ]; then
xauth_path="/var/run/lightdm/root/:0"
# 默认路径
else
xauth_path="/home/unitree/.Xauthority"
fi fi
fi fi
export XAUTHORITY="$xauth_path"
echo "Using XAUTHORITY: $XAUTHORITY"
export XAUTHORITY="$xauth_path" export XAUTHORITY="$xauth_path"
{ {