
Ich bin relativ neu bei Linux und neu im Bash-Scripting.
Ich versuche, auf meinem alten Asus EEE-Netbook eine Fn-F5-Taste einzurichten, um zwischen internen und externen Monitoren umzuschalten.
Erstellen eines Bash-Skripts in ~/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
Hotkey zum <keyboard>
Abschnitt hinzufügen ~/.config/openbox/lubuntu-rc.xml
:
...
<keybind key="XF86Display">
<action name="Execute">
<command>~/bin/toggle_displays.sh</command>
</action>
</keybind>
...
Das Problem ist sehr seltsam: Wenn das interne Display aktiv ist, funktioniert das Umschalten auf das externe Display immer. Aber wenn das externe Display aktiv ist, wird beim Umschalten nur ein schwarzer Bildschirm mit dem Mauszeiger auf dem internen Display angezeigt. Überraschender ist, dass letzteres Umschalten manchmal funktioniert, wenn ich das Skript vom Terminal aus ausführe, nicht per Hotkey.
Was kann das Problem sein? Und wie kann ich das Problem möglicherweise beheben?
Als Randbemerkung: Wenn ich ein Skript vom Terminal aus ausführe, um von der externen zur internen Anzeige zu wechseln, wird es LVDS1 connected primary (normal left inverted right x axis y axis)
auf dem Terminal gedruckt. Warum passiert das, wenn ich es nicht echo
in meinem Skript mache?
BEARBEITEN: Hier ist meine xrandr
Ausgabe (mit externem Monitor):
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)
Antwort1
Ich habe mir ein Bash-Skript geschrieben, das zwischen dem Bildschirm des Laptops und einem externen Bildschirm umschaltet. Es prüft, welcher Bildschirm eingeschaltet ist, schaltet ihn aus und schaltet den anderen Bildschirm mit seiner nativen Auflösung ein. Das Schöne daran ist, dass es die Namen der Bildschirme nicht kennen muss, da diese von xrandr gesammelt werden.
#!/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