解析 xrandr 输出以获取可用的分辨率

解析 xrandr 输出以获取可用的分辨率

我有如下 xrandr 输出

HDMI1 connected primary 1920x1080+0+0 (normal left inverted right x axis y axis) 480mm x 270mm
   1920x1080     60.00*+  50.00    59.94  
   1920x1080i    60.00    50.00    59.94  
   1680x1050     59.88  
   1400x1050     59.95  
   1600x900      60.00  
   1280x1024     75.02    60.02  
   1440x900      59.90  
   1280x800      59.91  
   1152x864      75.00  
   1280x720      60.00    50.00    59.94  
   1024x768      75.03    60.00  
   800x600       75.00    60.32  
   720x576       50.00  
   720x480       60.00    59.94  
   640x480       75.00    60.00    59.94  
   720x400       70.08  
HDMI2 disconnected (normal left inverted right x axis y axis)
VGA1 connected 1600x900+1920+0 (normal left inverted right x axis y axis) 440mm x 250mm
   1600x900      60.00*+
   1440x900      59.89  
   1280x800      59.81  
   1152x864      75.00  
   1280x720      60.00  
   1024x768      75.03    60.00  
   832x624       74.55  
   800x600       75.00    60.32    56.25  
   640x480       75.00    59.94  
   720x400       70.08  

如何编写 bash 脚本从上面的 xrandr 结果中获取可用的分辨率。

例如 the-script HDMI1将仅输出

HDMI1 connected primary 1920x1080+0+0 (normal left inverted right x axis y axis) 480mm x 270mm
   1920x1080     60.00*+  50.00    59.94  
   1920x1080i    60.00    50.00    59.94  
   1680x1050     59.88  
   1400x1050     59.95  
   1600x900      60.00  
   1280x1024     75.02    60.02  
   1440x900      59.90  
   1280x800      59.91  
   1152x864      75.00  
   1280x720      60.00    50.00    59.94  
   1024x768      75.03    60.00  
   800x600       75.00    60.32  
   720x576       50.00  
   720x480       60.00    59.94  
   640x480       75.00    60.00    59.94  
   720x400       70.08 

我努力了

xrandr | awk '/HDMI1.*?/,/.*connected [0-9].*/'

虽然结果接近我想要的,但是使用

xrandr | awk '/VGA1.*?/,/.*connected [0-9].*/'

没有列出VGA1输出下的分辨率。

任何帮助都值得感激,谢谢。

答案1

该脚本的 awk 命令的简化版本:

xrandr |
  awk -v monitor="^$MONITOR connected" '/connected/ {p = 0}
    $0 ~ monitor {p = 1}
    p'

从下到上:

  • p告诉 awk 根据其为真还是假来运行默认操作(打印)
  • 如果线路与显示器匹配且已连接,则设置p为 true
  • 对于所有其他监控线,我们将其设置p为 false

答案2

那就想想吧,

#!/bin/bash
#usage the-script XRANDR_OUTPUT_NAME
#e.g the-script HDMI1
MONITOR=$1;
xrandr | grep -v disconnected | \
awk '{ 
        if(/^'$MONITOR' connected/) { 
                print $0; 
                m="t"; 
        } else if(m == "t"){ 
                if (/^[a-zA-Z]/){
                        exit 
                } else { 
                        print $0
                } 
        }
}'

希望这对某些人有帮助。

答案3

使用 GNU awk:

#!/bin/sh

xrandr --query | gawk -v monitor="$1" '
  $0 ~ monitor && $0 !~ /disconnected/ {
    do {print}
    while (getline > -1 && $0 ~ /^[[:blank:]]/)
  }
'

用法:

$ ./the_script LVDS-1
LVDS-1 connected primary 1440x900+0+0 (normal left inverted right x axis y axis) 303mm x 190mm
   1440x900      60.00*+  59.89    50.00  
   1360x768      59.80    59.96  
   1152x864      60.00  
   1024x768      60.04    60.00  
   960x720       60.00  
   928x696       60.05  
   896x672       60.01  
   960x600       60.00  
   960x540       59.99  
   800x600       60.00    60.32    56.25  
   840x525       60.01    59.88  
   800x512       60.17  
   700x525       59.98  
   640x512       60.02  
   720x450       59.89  
   640x480       60.00    59.94  
   680x384       59.80    59.96  
   576x432       60.06  
   512x384       60.00  
   400x300       60.32    56.34  
   320x240       60.05  

如果你没有 GNU awk,你可能需要替换[[:blank:]][ \t]

相关内容