启动笔记本电脑时 Bash 脚本未运行

启动笔记本电脑时 Bash 脚本未运行

我是 Linux 新手,我已经按照自己的意愿设置了它。但是,当我在家使用笔记本电脑时,登录屏幕同时出现在笔记本电脑和外接屏幕上。我希望它只出现在我的主屏幕外接屏幕上。我做了一些研究,发现 sddm 管理器不知道哪个是我的主屏幕。所以我用/usr/share/sddm/scripts/Xsetup强制它仅显示在我的外接显示器上,但由于某种原因它不起作用。

#!/bin/sh 
# Xsetup - run as root before the login dialog appears


curr=$(xrandr | grep 'HDMI')

## Create a file to check if the script has been launched
echo "Script launched on $(date)" > /tmp/xrandScript.log


## Store the output of xrandr so you can check what the script sees:
xrandr >> /tmp/xrandScript.log

## print the value of the variable to see if it is what you expect
echo "The variable 'curr' has: $curr"  >> /tmp/xrandScript.log

#Nvidia graphics mode (gaming mode) xrandr | grep 'HDMI' result
h="HDMI-0 connected 1920x1080+0+0 (normal left inverted right x axis y axis) 509mm x 286mm"

#intel graphics (non gaming mode) xrandr | grep 'HDMI' result
l="HDMI-1-0 connected 1920x1080+0+0 (normal left inverted right x axis y axis) 509mm x 286mm"

if [ "$l" = "$curr" ]; then
        echo "Entered lower settings block" >> /tmp/xrandScript.log
        xrandr --output eDP-1 -off
        xrandr --output HDMI-1-0 --mode 1920x1080 --pos 0x0 --rotate normal
elif [ "$h" = "$curr" ]; then
        echo "Entered higher settings block" >> /tmp/xrandScript.log
        xrandr --output eDP-1-1 -off
        xrandr --output HDMI-0 --mode 1920x1080 --pos 0x0 --rotate normal
else
        echo "ERROR: unknown configuration: $curr"  >> /tmp/xrandScript.log
fi

/etc/sddm.conf

[XDisplay]
# Xsetup script path
# A script to execute when starting the display server
DisplayCommand=/usr/share/sddm/scripts/Xsetup

谁能告诉我我做错了什么?

编辑: Terdon 让我发布的调试信息:

Script launched on Wed Jan 13 15:00:03 CET 2021
Screen 0: minimum 320 x 200, current 1920 x 1080, maximum 16384 x 16384
eDP-1 connected primary 1920x1080+0+0 (normal left inverted right x axis y axis) 344mm x 194mm
   1920x1080     60.03*+  60.01    59.97    59.96    59.93  
   1680x1050     59.95    59.88  
   1600x1024     60.17  
   1400x1050     59.98  
   1600x900      59.99    59.94    59.95    59.82  
   1280x1024     60.02  
   1440x900      59.89  
   1400x900      59.96    59.88  
   1280x960      60.00  
   1440x810      60.00    59.97  
   1368x768      59.88    59.85  
   1360x768      59.80    59.96  
   1280x800      59.99    59.97    59.81    59.91  
   1152x864      60.00  
   1280x720      60.00    59.99    59.86    59.74  
   1024x768      60.04    60.00  
   960x720       60.00  
   928x696       60.05  
   896x672       60.01  
   1024x576      59.95    59.96    59.90    59.82  
   960x600       59.93    60.00  
   960x540       59.96    59.99    59.63    59.82  
   800x600       60.00    60.32    56.25  
   840x525       60.01    59.88  
   864x486       59.92    59.57  
   800x512       60.17  
   700x525       59.98  
   800x450       59.95    59.82  
   640x512       60.02  
   720x450       59.89  
   700x450       59.96    59.88  
   640x480       60.00    59.94  
   720x405       59.51    58.99  
   684x384       59.88    59.85  
   680x384       59.80    59.96  
   640x400       59.88    59.98  
   576x432       60.06  
   640x360       59.86    59.83    59.84    59.32  
   512x384       60.00  
   512x288       60.00    59.92  
   480x270       59.63    59.82  
   400x300       60.32    56.34  
   432x243       59.92    59.57  
   320x240       60.05  
   360x202       59.51    59.13  
   320x180       59.84    59.32  
DP-1-0 disconnected (normal left inverted right x axis y axis)
DP-1-1 disconnected (normal left inverted right x axis y axis)
DP-1-2 disconnected (normal left inverted right x axis y axis)
DP-1-3 disconnected (normal left inverted right x axis y axis)
HDMI-1-0 connected 1920x1080+0+0 (normal left inverted right x axis y axis) 509mm x 286mm
   1920x1080     60.00*+  59.94    50.00    29.97    25.00    23.98  
   1680x1050     59.95  
   1600x900      60.00  
   1440x900      59.89  
   1400x1050     59.98  
   1280x1024     60.02  
   1280x800      59.81  
   1280x720      60.00    59.94    50.00  
   1152x864      60.00  
   1024x768      60.00  
   800x600       60.32  
   720x576       50.00  
   720x480       59.94  
   640x480       59.94    59.93  
  1680x1050 (0x49) 146.250MHz -HSync +VSync
        h: width  1680 start 1784 end 1960 total 2240 skew    0 clock  65.29KHz
        v: height 1050 start 1053 end 1059 total 1089           clock  59.95Hz
  1440x900 (0x52) 106.500MHz -HSync +VSync
        h: width  1440 start 1520 end 1672 total 1904 skew    0 clock  55.93KHz
        v: height  900 start  903 end  909 total  934           clock  59.89Hz
  1280x1024 (0x51) 108.000MHz +HSync +VSync
        h: width  1280 start 1328 end 1440 total 1688 skew    0 clock  63.98KHz
        v: height 1024 start 1025 end 1028 total 1066           clock  60.02Hz
  1280x800 (0x5e) 83.500MHz -HSync +VSync
        h: width  1280 start 1352 end 1480 total 1680 skew    0 clock  49.70KHz
        v: height  800 start  803 end  809 total  831           clock  59.81Hz
  1152x864 (0x60) 81.620MHz -HSync +VSync
        h: width  1152 start 1216 end 1336 total 1520 skew    0 clock  53.70KHz
        v: height  864 start  865 end  868 total  895           clock  60.00Hz
  1024x768 (0x66) 65.000MHz -HSync -VSync
        h: width  1024 start 1048 end 1184 total 1344 skew    0 clock  48.36KHz
        v: height  768 start  771 end  777 total  806           clock  60.00Hz
  800x600 (0x75) 40.000MHz +HSync +VSync
        h: width   800 start  840 end  968 total 1056 skew    0 clock  37.88KHz
        v: height  600 start  601 end  605 total  628           clock  60.32Hz
  640x480 (0x84) 25.175MHz -HSync -VSync
        h: width   640 start  656 end  752 total  800 skew    0 clock  31.47KHz
        v: height  480 start  490 end  492 total  525           clock  59.94Hz
The variable 'curr' has: HDMI-1-0 connected 1920x1080+0+0 (normal left inverted right x axis y axis) 509mm x 286mm
Entered lower settings block

我想到了!

我写的xrandr --output eDP-1 -offxrandr --output eDP-1 --off

答案1

那不是 bash 脚本,而是一个sh脚本。在 Ubuntu 上,sh是一个非常基本的、符合 POSIX 标准的 shell,名为dash。但是,您的脚本正在使用 bash 特定的功能,因此sh无法运行它。最简单的解决方法是将 shebang 行更改为以下内容:

#!/bin/bash

或者,删除 bashisms。将 更改[[[和更改===

#!/bin/sh
curr=$(xrandr | grep 'HDMI')

#Nvidia graphics mode (gaming mode) xrandr | grep 'HDMI' result
h="HDMI-0 connected 1920x1080+0+0 (normal left inverted right x axis y axis) 509mm x 286mm"

#intel graphics (non gaming mode) xrandr | grep 'HDMI' result
l="HDMI-1-0 connected 1920x1080+0+0 (normal left inverted right x axis y axis) 509mm x 286mm"

if [ "$l" = "$curr" ]; then
        xrandr --output eDP-1 -off
        xrandr --output HDMI-1-0 --mode 1920x1080 --pos 0x0 --rotate normal
elif [ "$h" = "$curr" ]; then
        xrandr --output eDP-1-1 -off
        xrandr --output HDMI-0 --mode 1920x1080 --pos 0x0 --rotate normal
else
    echo "ERROR: unknown configuration: $curr"
fi

我还添加了 final else,以便您可以捕获意外行为,并更改了变量名称以避免使用大写字母。由于按照惯例,全局环境 shell 变量是大写的,因此在 shell 脚本中,最好不要将您自己的变量名称全部大写,以避免变量名称冲突。

如果这仍然不起作用,那么xrandr脚本运行时的输出可能不是您期望的。因此添加一些基本的调试:

#!/bin/sh
## Create a file to check if the script has been launched
echo "Script launched on $(date)" > /tmp/xrandScript.log


## Store the output of xrandr so you can check what the script sees:
xrandr >> /tmp/xrandScript.log

curr=$(xrandr | grep 'HDMI')

## print the value of the variable to see if it is what you expect
echo "The variable '$curr' has: $curr"  >> /tmp/xrandScript.log

#Nvidia graphics mode (gaming mode) xrandr | grep 'HDMI' result
h="HDMI-0 connected 1920x1080+0+0 (normal left inverted right x axis y axis) 509mm x 286mm"

#intel graphics (non gaming mode) xrandr | grep 'HDMI' result
l="HDMI-1-0 connected 1920x1080+0+0 (normal left inverted right x axis y axis) 509mm x 286mm"

if [ "$l" = "$curr" ]; then
        xrandr --output eDP-1 -off
        xrandr --output HDMI-1-0 --mode 1920x1080 --pos 0x0 --rotate normal
elif [ "$h" = "$curr" ]; then
        xrandr --output eDP-1-1 -off
        xrandr --output HDMI-0 --mode 1920x1080 --pos 0x0 --rotate normal
else
    echo "ERROR: unknown configuration: $curr"  >> /tmp/xrandScript.log
fi

现在,重新启动 X 服务器或者只是重新启动以便启动脚本并检查文件是否/tmp/xrandScript.log已创建以及其内容是否符合您的预期。

相关内容