Chinaunix首页 | 论坛 | 博客
  • 博客访问: 397348
  • 博文数量: 81
  • 博客积分: 45
  • 博客等级: 民兵
  • 技术积分: 608
  • 用 户 组: 普通用户
  • 注册时间: 2012-10-22 11:46
个人简介

一个愤青

文章分类

全部博文(81)

文章存档

2015年(40)

2014年(29)

2013年(11)

2012年(1)

我的朋友

分类: LINUX

2014-09-02 01:19:46


点击(此处)折叠或打开

  1. #!/bin/bash

  2. function usage() {
  3.     pro=$(basename $0)
  4.     echo "Welcome to $pro, version 0.1"
  5.     echo "code by slucx(slucx@sina.com)"
  6.     echo "$pro [inside/outside/all/toggle/help]"
  7.     echo "arguments:"
  8.     echo -e "\t inside - use inside monitor"
  9.     echo -e "\t outside - use outside monitor"
  10.     echo -e "\t all - use both inside and outside monitors"
  11.     echo -e "\t toggle - toggle the monitor"
  12.     echo -e "\t help - print this document"
  13. }

  14. # set backgroud
  15. eval $(cat ~/.fehbg)

  16. # vars
  17. inside=0
  18. outside=1
  19. all=2
  20. mm="/tmp/monitor.chg"

  21. # devices
  22. dev_inside="LVDS1"
  23. if [[ $(xrandr | grep "HDMI1 connected") ]]; then
  24.     dev_outside="HDMI1"
  25. elif [[ $(xrandr | grep "VGA1 connected") ]]; then
  26.     dev_outside="VGA1"
  27. else
  28.     dev_outside=$dev_inside
  29. fi

  30. # if not exist, create it
  31. [[ -f $mm ]] || echo $inside > $mm

  32. # set value
  33. v=$(cat $mm)
  34. if [ $# == 1 ]; then
  35.     case $1 in
  36.     "inside")
  37.      [ $v == $inside ] && exit
  38.      v=$inside
  39.      echo $inside > $mm
  40.      ;;
  41.     "outside")
  42.      [ $v == $outside ] && exit
  43.      v=$outside
  44.      echo $outside > $mm
  45.      ;;
  46.     "all")
  47.      [ $v == $all ] && exit
  48.      v=$all
  49.      echo $all > $mm
  50.      ;;
  51.     "toggle")
  52.      v=$(($v + 1))
  53.      [[ $v -le $outside ]] || v=$inside
  54.      echo $v > $mm
  55.      ;;
  56.     "help")
  57.      usage
  58.      exit
  59.      ;;
  60.     *)
  61.      echo "Unknown command '$1'..."
  62.      usage
  63.      exit
  64.      ;;
  65.     esac
  66. elif [ $# == 0 ]; then        # use outside monitor first
  67.     v=$outside
  68.     echo $outside > $mm
  69. fi

  70. # change monitor
  71. case $v in
  72.     $all)
  73.     if [ "$dev_outside"x != "$dev_inside"x ]; then
  74.      echo "using all"
  75.      xrandr --output $dev_outside --left-of $dev_inside --auto --output $dev_inside --auto
  76.     fi
  77.     ;;
  78.     $outside)
  79.     if [ "$dev_outside"x != "$dev_inside"x ]; then
  80.      echo "using outside monitor"
  81.      xrandr --output $dev_outside --auto --output $dev_inside --off
  82.     fi
  83.     ;;
  84.     $inside)
  85.     echo "using inside monitor"
  86.     xrandr --output $dev_inside --auto
  87.     if [ "$dev_outside"x != "$dev_inside"x ]; then
  88.      xrandr --output $dev_outside --off
  89.     fi
  90.     ;;
  91.     *)
  92.     ;;
  93. esac


用法: 假设脚本名称为monitor.sh
monitor.sh inside 使用内部显示器
monitor.sh outside 使用外部显示器
monitor.sh all 同时使用,外部显示器在左
monitor.sh 尝试优先使用外部显示器
monitor.sh toggle 切换内部和外部显示器

注:显示器设备名称可能不一致,具体的可以通过xrandr查询
阅读(1199) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~