如何使用 shell 仅检索其他单词之前的 2 个单词?

How to retrieve only 2 words before other ones using shell?

我正在为 xrandr 创建一个自动化脚本,我只想从中检索实际输出,我知道它应该是 connected 字之前的两个字(例如 DP-0)使用 xrandr --verbose:

xrandr --verbose

Screen 0: minimum 8 x 8, current 3840 x 2160, maximum 32767 x 32767
DVI-D-0 disconnected (normal left inverted right x axis y axis)
    Identifier: 0x1bd
    Timestamp:  2846939
    Subpixel:   unknown
    Clones:    
    CRTCs:      0 1 2 3
    Transform:  1.000000 0.000000 0.000000
                0.000000 1.000000 0.000000
                0.000000 0.000000 1.000000
               filter: 
    CscMatrix: 65536 0 0 0 0 65536 0 0 0 0 65536 0 
    BorderDimensions: 4 
        supported: 4
    Border: 0 0 0 0 
        range: (0, 65535)
    SignalFormat: TMDS 
        supported: TMDS
    ConnectorType: DVI-D 
    ConnectorNumber: 0 
    _ConnectorLocation: 0 
    non-desktop: 0 
        supported: 0, 1
HDMI-0 disconnected (normal left inverted right x axis y axis)
    Identifier: 0x1be
    Timestamp:  2846939
    Subpixel:   unknown
    Clones:    
    CRTCs:      0 1 2 3
    Transform:  1.000000 0.000000 0.000000
                0.000000 1.000000 0.000000
                0.000000 0.000000 1.000000
               filter: 
    CscMatrix: 65536 0 0 0 0 65536 0 0 0 0 65536 0 
    BorderDimensions: 4 
        supported: 4
    Border: 0 0 0 0 
        range: (0, 65535)
    SignalFormat: TMDS 
        supported: TMDS
    ConnectorType: HDMI 
    ConnectorNumber: 3 
    _ConnectorLocation: 3 
    non-desktop: 0 
        supported: 0, 1
HDMI-1 disconnected (normal left inverted right x axis y axis)
    Identifier: 0x1bf
    Timestamp:  2846939
    Subpixel:   unknown
    Clones:    
    CRTCs:      0 1 2 3
    Transform:  1.000000 0.000000 0.000000
                0.000000 1.000000 0.000000
                0.000000 0.000000 1.000000
               filter: 
    CscMatrix: 65536 0 0 0 0 65536 0 0 0 0 65536 0 
    BorderDimensions: 4 
        supported: 4
    Border: 0 0 0 0 
        range: (0, 65535)
    SignalFormat: TMDS 
        supported: TMDS
    ConnectorType: HDMI 
    ConnectorNumber: 4 
    _ConnectorLocation: 4 
    non-desktop: 0 
        supported: 0, 1
DP-0 connected primary 3840x2160+0+0 (0x1c1) normal (normal left inverted right x axis y axis) 526mm x 296mm
    Identifier: 0x1c0
    Timestamp:  2846939
    Subpixel:   unknown
    Gamma:      1.0:1.0:1.0
    Brightness: 1.0
    Clones:    
    CRTC:       0
    CRTCs:      0 1 2 3
    Transform:  1.500000 0.000000 0.000000
                0.000000 1.500000 0.000000
                0.000000 0.000000 1.000000
               filter: bilinear
    _MUTTER_PRESENTATION_OUTPUT: 0 
    CscMatrix: 65536 0 0 0 0 65536 0 0 0 0 65536 0 
    EDID: 
        00ffffffffffff0010acc1d054504130
        2c1b0104a5351e783aad75a9544d9d26
        0f5054a54b008100b300d100714fa940
        8180d1c00101565e00a0a0a029503020
        35000e282100001a000000ff0032394a
        3050374233304150540a000000fc0044
        454c4c205032343138440a20000000fd
        0031561d711c010a20202020202001d4
        020315b1509005040302071601061112
        1513141f20023a801871382d40582c45
        000e282100001e011d8018711c162058
        2c25000e282100009ebf1600a0803813
        4030203a000e282100001a7e3900a080
        381f4030203a000e282100001a000000
        00000000000000000000000000000000
        00000000000000000000000000000062
    BorderDimensions: 4 
        supported: 4
    Border: 0 0 0 0 
        range: (0, 65535)
    SignalFormat: DisplayPort 
        supported: DisplayPort
    ConnectorType: DisplayPort 
    ConnectorNumber: 2 
    _ConnectorLocation: 2 
    non-desktop: 0 
        supported: 0, 1
  2560x1440 (0x1c1) 241.500MHz +HSync -VSync *current +preferred
        h: width  2560 start 2608 end 2640 total 2720 skew    0 clock  88.79KHz
        v: height 1440 start 1443 end 1448 total 1481           clock  59.95Hz
  2048x1080 (0x1c2) 147.180MHz +HSync -VSync
        h: width  2048 start 2096 end 2128 total 2208 skew    0 clock  66.66KHz
        v: height 1080 start 1083 end 1093 total 1111           clock  60.00Hz
  2048x1080 (0x1c3) 58.230MHz +HSync -VSync
        h: width  2048 start 2096 end 2128 total 2208 skew    0 clock  26.37KHz
        v: height 1080 start 1083 end 1093 total 1099           clock  24.00Hz
  1920x1200 (0x1c4) 193.250MHz -HSync +VSync
        h: width  1920 start 2056 end 2256 total 2592 skew    0 clock  74.56KHz
        v: height 1200 start 1203 end 1209 total 1245           clock  59.88Hz
  1920x1080 (0x1c5) 148.500MHz +HSync +VSync
        h: width  1920 start 2008 end 2052 total 2200 skew    0 clock  67.50KHz
        v: height 1080 start 1084 end 1089 total 1125           clock  60.00Hz
  1920x1080 (0x1c6) 148.350MHz +HSync +VSync
        h: width  1920 start 2008 end 2052 total 2200 skew    0 clock  67.43KHz
        v: height 1080 start 1084 end 1089 total 1125           clock  59.94Hz
  1920x1080 (0x1c7) 148.500MHz +HSync +VSync
        h: width  1920 start 2448 end 2492 total 2640 skew    0 clock  56.25KHz
        v: height 1080 start 1084 end 1089 total 1125           clock  50.00Hz
  1920x1080 (0x1c8) 74.180MHz +HSync +VSync
        h: width  1920 start 2558 end 2602 total 2750 skew    0 clock  26.97KHz
        v: height 1080 start 1084 end 1089 total 1125           clock  23.98Hz
  1680x1050 (0x1c9) 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
  1600x1200 (0x1ca) 162.000MHz +HSync +VSync
        h: width  1600 start 1664 end 1856 total 2160 skew    0 clock  75.00KHz
        v: height 1200 start 1201 end 1204 total 1250           clock  60.00Hz
  1280x1024 (0x1cb) 135.000MHz +HSync +VSync
        h: width  1280 start 1296 end 1440 total 1688 skew    0 clock  79.98KHz
        v: height 1024 start 1025 end 1028 total 1066           clock  75.02Hz
  1280x1024 (0x1cc) 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 (0x1cd) 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
  1280x720 (0x1ce) 74.180MHz +HSync +VSync
        h: width  1280 start 1390 end 1430 total 1650 skew    0 clock  44.96KHz
        v: height  720 start  725 end  730 total  750           clock  59.94Hz
  1280x720 (0x1cf) 74.250MHz +HSync +VSync
        h: width  1280 start 1720 end 1760 total 1980 skew    0 clock  37.50KHz
        v: height  720 start  725 end  730 total  750           clock  50.00Hz
  1152x864 (0x1d0) 108.000MHz +HSync +VSync
        h: width  1152 start 1216 end 1344 total 1600 skew    0 clock  67.50KHz
        v: height  864 start  865 end  868 total  900           clock  75.00Hz
  1024x768 (0x1d1) 78.750MHz +HSync +VSync
        h: width  1024 start 1040 end 1136 total 1312 skew    0 clock  60.02KHz
        v: height  768 start  769 end  772 total  800           clock  75.03Hz
  1024x768 (0x1d2) 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 (0x1d3) 49.500MHz +HSync +VSync
        h: width   800 start  816 end  896 total 1056 skew    0 clock  46.88KHz
        v: height  600 start  601 end  604 total  625           clock  75.00Hz
  800x600 (0x1d4) 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
  720x576 (0x1d5) 27.000MHz -HSync -VSync
        h: width   720 start  732 end  796 total  864 skew    0 clock  31.25KHz
        v: height  576 start  581 end  586 total  625           clock  50.00Hz
  720x480 (0x1d6) 27.000MHz -HSync -VSync
        h: width   720 start  736 end  798 total  858 skew    0 clock  31.47KHz
        v: height  480 start  489 end  495 total  525           clock  59.94Hz
  640x480 (0x1d7) 31.500MHz -HSync -VSync
        h: width   640 start  656 end  720 total  840 skew    0 clock  37.50KHz
        v: height  480 start  481 end  484 total  500           clock  75.00Hz
  640x480 (0x1d8) 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
  640x480 (0x1d9) 25.170MHz -HSync -VSync
        h: width   640 start  656 end  752 total  800 skew    0 clock  31.46KHz
        v: height  480 start  490 end  492 total  525           clock  59.93Hz
DP-1 disconnected (normal left inverted right x axis y axis)
    Identifier: 0x1da
    Timestamp:  2846939
    Subpixel:   unknown
    Clones:    
    CRTCs:      0 1 2 3
    Transform:  1.000000 0.000000 0.000000
                0.000000 1.000000 0.000000
                0.000000 0.000000 1.000000
               filter: 
    CscMatrix: 65536 0 0 0 0 65536 0 0 0 0 65536 0 
    BorderDimensions: 4 
        supported: 4
    Border: 0 0 0 0 
        range: (0, 65535)
    SignalFormat: TMDS 
        supported: TMDS
    ConnectorType: DisplayPort 
    ConnectorNumber: 2 
    _ConnectorLocation: 2 
    non-desktop: 0 
        supported: 0, 1
DP-2 disconnected (normal left inverted right x axis y axis)
    Identifier: 0x1db
    Timestamp:  2846939
    Subpixel:   unknown
    Clones:    
    CRTCs:      0 1 2 3
    Transform:  1.000000 0.000000 0.000000
                0.000000 1.000000 0.000000
                0.000000 0.000000 1.000000
               filter: 
    CscMatrix: 65536 0 0 0 0 65536 0 0 0 0 65536 0 
    BorderDimensions: 4 
        supported: 4
    Border: 0 0 0 0 
        range: (0, 65535)
    SignalFormat: DisplayPort 
        supported: DisplayPort
    ConnectorType: DisplayPort 
    ConnectorNumber: 1 
    _ConnectorLocation: 1 
    non-desktop: 0 
        supported: 0, 1
DP-3 disconnected (normal left inverted right x axis y axis)
    Identifier: 0x1dc
    Timestamp:  2846939
    Subpixel:   unknown
    Clones:    
    CRTCs:      0 1 2 3
    Transform:  1.000000 0.000000 0.000000
                0.000000 1.000000 0.000000
                0.000000 0.000000 1.000000
               filter: 
    CscMatrix: 65536 0 0 0 0 65536 0 0 0 0 65536 0 
    BorderDimensions: 4 
        supported: 4
    Border: 0 0 0 0 
        range: (0, 65535)
    SignalFormat: TMDS 
        supported: TMDS
    ConnectorType: DisplayPort 
    ConnectorNumber: 1 
    _ConnectorLocation: 1 
    non-desktop: 0 
        supported: 0, 1

[更新] 最终代码(使用了touch,因为我无法分配内容并仅使用创建新文件echocat EOFtee)

#!/bin/bash

#A Xorg/Gnome oriented bash script that creates a virtual resolution of 4k scaled to 2k in order to simulate a 'retina' display for 2k displays


#Creates the needed files: resolution_fix.sh and fix_resolution.desktop
touch $HOME/resolution_fix.sh $HOME/.config/autostart/fix_resolution.desktop

DISPLAY=$(xrandr --verbose 2>/dev/null | grep connected | grep -v disconnected | cut -d' ' -f1)

cat <<EOF > $HOME/resolution_fix.sh
#2x UI(User Interface) scaling
gsettings set org.gnome.desktop.interface scaling-factor 2
gsettings set org.gnome.settings-daemon.plugins.xsettings overrides "[{'Gdk/WindowScalingFactor', <2>}]"
#Virtual resolution of 4k scaled to 2k.
xrandr --output $DISPLAY --panning 3840x2160 --scale 1.5x1.5
EOF


# Gives execute permission to all users
chmod +x $HOME/resolution_fix.sh

# Creates an autostart script for the resolution adjustment
cat <<EOF > $HOME/.config/autostart/fix_resolution.desktop
[Desktop Entry]
Name=ResolutionFix
Comment=Retina resolution for 2k display
Exec=$HOME/resolution_fix.sh
Terminal=false
Type=Application
X-GNOME-Autostart-enabled=true
EOF

非常感谢!

xrandr --verbose|grep connected|grep -v disconnected|cut -d' ' -f1

用空格分隔的时候真的是1个字

警告:您可能有不止一台显示器。

或none。 "Can't open display" 可能需要重定向或检测。

慢慢构建,"connected"可能是一个子字符串。

正在分配:

DISP=$(xrandr --verbose 2>/dev/null | grep connected | grep -v disconnected | cut -d' ' -f1)