switchmonitors 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/bin/bash
  2. #
  3. # toggle-display.sh
  4. #
  5. # Iterates through connected monitors in xrander and switched to the next one
  6. # each time it is run.
  7. #
  8. # get info from xrandr
  9. xStatus=`xrandr`
  10. connectedOutputs=$(echo "$xStatus" | grep " connected" | sed -e "s/\([A-Z0-9]\+\) connected.*/\1/")
  11. activeOutput=$(echo "$xStatus" | grep -e " connected [^(]" | sed -e "s/\([A-Z0-9]\+\) connected.*/\1/")
  12. connected=$(echo $connectedOutputs | wc -w)
  13. # initialize variables
  14. execute="xrandr "
  15. default="xrandr "
  16. i=1
  17. switch=0
  18. for display in $connectedOutputs
  19. do
  20. # build default configuration
  21. if [ $i -eq 1 ]
  22. then
  23. default=$default"--output $display --auto "
  24. else
  25. default=$default"--output $display --off "
  26. fi
  27. # build "switching" configuration
  28. if [ $switch -eq 1 ]
  29. then
  30. execute=$execute"--output $display --auto "
  31. switch=0
  32. else
  33. execute=$execute"--output $display --off "
  34. fi
  35. # check whether the next output should be switched on
  36. if [ $display = $activeOutput ]
  37. then
  38. switch=1
  39. fi
  40. i=$(( $i + 1 ))
  41. done
  42. # check if the default setup needs to be executed then run it
  43. echo "Resulting Configuration:"
  44. if [ -z "$(echo $execute | grep "auto")" ]
  45. then
  46. echo "Command: $default"
  47. `$default`
  48. else
  49. echo "Command: $execute"
  50. `$execute`
  51. fi
  52. echo -e "\n$(xrandr)"