I am relatively new to Linux and new to bash scripting.
I am trying to set up a Fn-F5 key on my old Asus EEE netbook to toggle between internal and external monitors.
Creating a bash script 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
Adding hotkey to <keyboard>
section of ~/.config/openbox/lubuntu-rc.xml
:
...
<keybind key="XF86Display">
<action name="Execute">
<command>~/bin/toggle_displays.sh</command>
</action>
</keybind>
...
The problem is very strange: When internal display is active, switching to external always works. But when external is active, switching just makes a black screen with mouse cursor on internal display. More surprisingly, the latter switch sometimes works if I run the script from terminal, not with hotkey.
What can be the problem? And how could I possibly debug this?
As a side note, running script from terminal to switch from external to internal display prints LVDS1 connected primary (normal left inverted right x axis y axis)
to terminal. Why does it happen if I don't echo
in my script?
EDIT:
Here is my xrandr
output (using external 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)