4

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)

1 Answer 1

1

I wrote myself a bash script that switches between the laptop's screen and an external screen. It checks which screen is on, switches it off and switches the other screen on with its native resolution. The nice thing is. that it doesn't need to know the names of the screens as those are gathered from 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.