
我對 Linux 和 bash 腳本都比較陌生。
我正在嘗試在我的舊華碩 EEE 上網本上設定 Fn-F5 鍵以在內部和外部顯示器之間切換。
在以下位置建立 bash 腳本~/bin/toggle_displays.sh
:
#!/bin/bash
if (xrandr | grep "VGA1 disconnected"); then
# external monitor not connected: do nothing
xrandr --output LVDS1 --auto --output VGA1 --off
else
# external monitor connected: toggle monitors
if (xrandr | grep -E "LVDS1 connected (primary )?\("); then
xrandr --output LVDS1 --auto --output VGA1 --off
else
xrandr --output LVDS1 --off --output VGA1 --auto
fi
fi
將熱鍵添加到<keyboard>
以下部分~/.config/openbox/lubuntu-rc.xml
:
...
<keybind key="XF86Display">
<action name="Execute">
<command>~/bin/toggle_displays.sh</command>
</action>
</keybind>
...
問題很奇怪:當內部顯示器處於活動狀態時,切換到外部顯示器總是有效。但是,當外部處於活動狀態時,切換只會使內部顯示器上出現滑鼠遊標黑屏。更令人驚訝的是,如果我從終端而不是使用熱鍵運行腳本,後一個開關有時會起作用。
可能是什麼問題?我怎麼可能調試這個?
作為旁注,從終端運行腳本以從外部顯示切換到內部顯示列印LVDS1 connected primary (normal left inverted right x axis y axis)
到終端。如果我echo
的腳本中沒有這樣做,為什麼會發生這種情況?
編輯:這是我的xrandr
輸出(使用外部顯示器):
Screen 0: minimum 8 x 8, current 1680 x 1050, maximum 32767 x 32767
LVDS1 connected primary (normal left inverted right x axis y axis)
1024x600 60.0 +
800x600 60.3 56.2
640x480 59.9
VGA1 connected 1680x1050+0+0 (normal left inverted right x axis y axis) 473mm x 296mm
1680x1050 60.0*+
1600x1200 60.0
1280x1024 75.0 60.0
1440x900 75.0 59.9
1280x800 59.8
1152x864 75.0
1024x768 75.1 70.1 60.0
832x624 74.6
800x600 72.2 75.0 60.3 56.2
640x480 75.0 72.8 66.7 60.0
720x400 70.1
VIRTUAL1 disconnected (normal left inverted right x axis y axis)
答案1
我自己編寫了一個 bash 腳本,用於在筆記型電腦的螢幕和外部螢幕之間切換。它檢查哪個螢幕打開,將其關閉並以其原始解析度打開另一個螢幕。好處是。它不需要知道螢幕的名稱,因為這些名稱是從 xrandr 收集的。
#!/bin/bash
#Toggles between two screens. Assuming only one external screen is connected
monitors=`xrandr | grep -P ' connected' | grep -o '^[^ ]*'`
set -- "$monitors"
#monArr contains an array of the id's of the connected screens
declare -a monArr=($*)
#onMon holds the id of the *first* on screen found
onMon=`xrandr --listmonitors | head -2 | grep -oP "[a-zA-Z]*-[0-9]$"`
#offMon holds the id of the other monitor, which is not on
if [ "$onMon" = "${monArr[0]}" ]
then
offMon=${monArr[1]}
else
offMon=${monArr[0]}
fi
#Switches the on screen off and vice versa
xrandr --output $offMon --auto --output $onMon --off