受到贝尼的回答的启发,这将阅读EDID http://en.wikipedia.org/wiki/EDID数据使用xrandr
并根据以下内容提取监视器名称EDID规范 http://read.pudn.com/downloads110/ebook/456020/E-EDID%20Standard.pdf,不需要任何外部工具,例如parse-edid
:
#!/bin/bash
while read -r output hex conn; do
[[ -z "$conn" ]] && conn=${output%%-*}
echo "# $output $conn $(xxd -r -p <<< "$hex")"
done < <(xrandr --prop | awk '
!/^[ \t]/ {
if (output && hex) print output, hex, conn
output=$1
hex=""
}
/ConnectorType:/ {conn=$2}
/[:.]/ && h {
sub(/.*000000fc00/, "", hex)
hex = substr(hex, 0, 26) "0a"
sub(/0a.*/, "", hex)
h=0
}
h {sub(/[ \t]+/, ""); hex = hex $0}
/EDID.*:/ {h=1}
END {if (output && hex) print output, hex, conn}
' | sort
)
Uses awk
精确提取监视器名称only,并且 EDID 中没有额外的垃圾,因此“幻数”如000000fc00
, 26
and 0a
。最后使用xxd
从十六进制转换为 ASCII,每行打印一个监视器名称。
基于这个解决方案我做了一个切换显示器的便捷脚本 https://github.com/MestreLion/scripts/blob/master/monitor-switch,也可以用来简单地列出监视器信息:
$ monitor-switch --list
Connected monitors:
# DFP5 HDMI HT-R391
# DFP7 DVI-I DELL U2412M
$ monitor-switch --list
Connected monitors:
# DisplayPort-1 DisplayPort DELL U2412M
# DisplayPort-3 DisplayPort DELL U2415
# HDMI-A-2 HDMI LG TV