情况:我正在使用多个显示器,我想用bash来命名。目前我正在使用Ubuntu 10.04。
我知道xrandr。从中我只能得到统计数据。我想要的是读取数组中的所有监视器名称以使用它们。
在没有从某种字符串中删除名称的情况下,是否有明确的方法可以做到这一点?一个明确的方法是从文件中读取它们。一个不太明确的方法是将xrandr输出管道输出到某种类型的函数来从中删除名称。
答案 0 :(得分:14)
受Beni的回答启发,这将使用xrandr
读取EDID数据并根据EDID specification提取监视器名称,而无需任何外部工具parse-edid
:
#!/bin/sh
xrandr --verbose | awk '
/[:.]/ && hex {
sub(/.*000000fc00/, "", hex)
hex = substr(hex, 0, 26) "0a"
sub(/0a.*/, "0a", hex)
print hex
hex=""
}
hex {
gsub(/[ \t]+/, "")
hex = hex $0
}
/EDID.*:/ {
hex=" "
}' | xxd -r -p
使用awk
精确提取显示器名称仅,并且没有来自EDID的额外垃圾,因此"魔术数字"例如000000fc00
,26
和0a
。最后使用xxd
将十六进制转换为ASCII,每行打印一个监视器名称。
根据这个解决方案,我制作了一个handy script to switch monitors,它也可以用来简单地列出监听信息:
$ monitor-switch --list
Connected monitors:
# DFP5 HDMI HT-R391
# DFP7 DVI-I DELL U2412M
答案 1 :(得分:9)
sudo get-edid
对我不起作用。 (编辑:现在可以在另一台计算机上工作,Lubuntu 14.10;我会责怪BIOS差异,但这是一个随机猜测...)
无论如何,在{X}下,xrandr --verbose
打印出EDID块。这是一种快速而肮脏的方式来提取它并传递给parse-edid
:
#!/bin/bash
xrandr --verbose | perl -ne '
if ((/EDID(_DATA)?:/.../:/) && !/:/) {
s/^\s+//;
chomp;
$hex .= $_;
} elsif ($hex) {
# Use "|strings" if you dont have read-edid package installed
# and just want to see (or grep) the human-readable parts.
open FH, "|parse-edid";
print FH pack("H*", $hex);
$hex = "";
}'
答案 2 :(得分:8)
在Ubuntu 16.04,18.04上测试。 (我知道回答太晚了,但今天这个解决方案很有用)
$ sudo apt-get install -y hwinfo
...
$ hwinfo --monitor --short
monitor:
SONY TV
AUO LCD Monitor
我有两台显示器。一台配备笔记本电脑,另一台配备外接显示器。只要外接显示器插入或拔出,此命令就会反映出更改。你不断需要进行民意调查。删除--short
选项可提供更详细的信息。
您可以使用以下后台作业轮询状态:
$ while true;
> do
> hwinfo --monitor --short;
> sleep 2;
> done >> monitor.log &
while true
循环无限次运行。 sleep 2
暂停循环的每次迭代2秒。 hwinfo --monitor --short
的输出会附加到monitor.log
。此日志文件可以为您提供监视器插件和插件的活动历史记录。
仅供参考:我正在使用上述命令(以及其他类似命令)使用后台(守护程序)python脚本来检测是否有人正在使用计算机实验室中的系统执行某些硬件插件和插件。如果是这样,我会得到适当的通知,告知有人几乎实时地插入显示器,鼠标或键盘!
答案 3 :(得分:2)
如果您不想解析xrandr
输出,请使用libXrandr
编写一个只能获得所需内容的C程序。如果您只想查询信息,可以快速完成。 Read this document
如果你想获得真正的监视器名称,@ dtmilano解决方案的替代方法是使用libXrandr获取监视器的EDID属性,然后手动解析它并打印(阅读EDID规范)
答案 4 :(得分:2)
我知道这是一种肮脏的方式,但它给我一些监视器型号名称甚至比sudo get-edid|parse-edid
更好。它以数组形式读取信息,并以可读取的方式输出,就像读取文件一样。您可以根据需要进行修改。
#!/bin/bash
#
#
# get-monitors.sh
#
# Get monitor name and some other properties of connected monitors
# by investigating the output of xrandr command and EDID data
# provided by it.
#
# Copyright (C) 2015,2016 Jarno Suni <8@iki.fi>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. See <http://www.gnu.org/licenses/gpl.html>
set -o nounset
set -o errexit
# EDID format:
# http://en.wikipedia.org/wiki/Extended_Display_Identification_Data#EDID_1.3_data_format
# http://read.pudn.com/downloads110/ebook/456020/E-EDID%20Standard.pdf
declare -r us=';' # separator string;
# If EDID has more than one field with same tag, concatenate them,
# but add this string in between.
declare -r fs=$'\x1f' # Field separator for internal use;
# must be a character that does not occur in data fields.
declare -r invalid_edid_tag='--bad EDID--'
# If base EDID is invalid, don't try to extract information from it,
# but assign this string to the fields.
# Get information in these arrays:
declare -a outs # Output names
declare -a conns # Connection type names (if available)
declare -a names # Monitor names (but empty for some laptop displays)
declare -a datas # Extra data; may include laptop display brand name
# and model name
declare -i no # number of connected outputs (to be counted)
# xrandr command to use as a source of information:
declare -r xrandr_output_cmd="xrandr --prop"
hex_to_ascii() {
echo -n "$1" | xxd -r -p
}
ascii_to_hex() {
echo -n "$1" | xxd -p
}
get_info() {
no=0
declare OIFS=$IFS;
IFS=$fs
while read -r output conn hexn hexd; do
outs[no]="${output}"
conns[no]="${conn}"
names[no]="$(hex_to_ascii "$hexn")"
datas[no]="$(hex_to_ascii "$hexd")"
(( ++no ))
done < <(eval $xrandr_output_cmd | gawk -v gfs="$fs" '
function print_fields() {
print output, conn, hexn, hexd
conn=""; hexn=""; hexd=""
}
function append_hex_field(src_hex,position,app_hex, n) {
n=substr(src_hex,position+10,26)
sub(/0a.*/, "", n)
# EDID specification says field ends by 0x0a
# (\n), if it is shorter than 13 bytes.
#sub(/(20)+$/, "", n)
# strip whitespace at the end of ascii string
if (n && app_hex) return app_hex sp n
else return app_hex n
}
function get_hex_edid( hex) {
getline
while (/^[ \t]*[[:xdigit:]]+$/) {
sub(/[ \t]*/, "")
hex = hex $0
getline
}
return hex
}
function valid_edid(hex, a, sum) {
if (length(hex)<256) return 0
for ( a=1; a<=256; a+=2 ) {
# this requires gawk
sum+=strtonum("0x" substr(hex,a,2))
# this requires --non-decimal-data for gawk:
#sum+=sprintf("%d", "0x" substr(hex,a,2))
}
if (sum % 256) return 0
return 1
}
BEGIN {
OFS=gfs
}
/[^[:blank:]]+ connected/ {
if (unprinted) print_fields()
unprinted=1
output=$1
}
/[^[:blank:]]+ disconnected/ {
if (unprinted) print_fields()
unprinted=0
}
/^[[:blank:]]*EDID.*:/ {
hex=get_hex_edid()
if (valid_edid(hex)) {
for ( c=109; c<=217; c+=36 ) {
switch (substr(hex,c,10)) {
case "000000fc00" :
hexn=append_hex_field(hex,c,hexn)
break
case "000000fe00" :
hexd=append_hex_field(hex,c,hexd)
break
}
}
} else {
# set special value to denote invalid EDID
hexn=iet; hexd=iet
}
}
/ConnectorType:/ {
conn=$2
}
END {
if (unprinted) print_fields()
}' sp=$(ascii_to_hex $us) iet=$(ascii_to_hex $invalid_edid_tag))
IFS="$OIFS"
}
get_info
# print the colums of each display quoted in one row
for (( i=0; i<$no; i++ )); do
echo "'${outs[i]}' '${conns[i]}' '${names[i]}' '${datas[i]}'"
done
答案 5 :(得分:1)
您可以尝试ddcprobe
和/或get-edid
$ sudo apt-get install xresprobe read-edid
$ sudo ddcprobe
$ sudo get-edid
答案 6 :(得分:0)
您正在查找EDID信息,该信息通过I²C总线传递并由您的视频驱动程序解释。正如dtmilano所说,ddcprobe的get-edit应该有效。
您还可以通过记录X start来获取此信息:
startx -- -logverbose 6
多年前,我使用了一个名为read-edid的软件包来收集这些信息。
自2009年以来,已经在Ubuntu中提供了read-edid包,according to this blog post。