Gnome3 - 从命令行(脚本)设置屏幕能量设置

Gnome3 - 从命令行(脚本)设置屏幕能量设置

有没有办法从命令行访问、设置 gnome 系统设置?

我对一种设置方式特别感兴趣:

settings -> energy -> screen off = never
settings -> energy -> screen off = (N) minue(s)

我想从 bash 脚本访问此设置,因此我可以将屏幕关闭设置为never脚本启动时并返回到(N) minue(s)脚本退出之前。

我希望我的问题很清楚,因为我刚刚从德语 GUI 翻译了菜单层次结构。

我试图找到一个设置,gsettings但无法真正完成。

答案1

好的,我找到了,这非常好,因为它已设置,seconds您还可以设置一个比一分钟更短的值:

# Set "turn screen off" to (N)seconds:
  gsettings set org.gnome.desktop.session idle-delay N

# Set "turn screen off" to never:
  gsettings set org.gnome.desktop.session idle-delay 0

# Set "turn screen off" to 1 minute (60)seconds:
  gsettings set org.gnome.desktop.session idle-delay 60

在脚本中我按如下方式使用它:

# getting the time of current setting and store to variable 'screenoff'
  screenoff=$(gsettings get org.gnome.desktop.session idle-delay \
               | awk '{print $2}')

# set "turn screen off" to never
  gsettings set org.gnome.desktop.session idle-delay 0

  echo "lots of important information being printed..."

# set back to the former setting:
  gsettings set org.gnome.desktop.session idle-delay "$screenoff"

$screenoff如果脚本在完成之前被取消,存储在其中的值当然会丢失。


解决方法可能是将值存储到 a temporary file,它位于removed脚本的最后:

#!/bin/bash

# check if there is a temporary file from an aborted session
# read from this file or get value from gsettings & write temporay file:

  if [ -f "./.screenoff.tmp" ]; then
      screenoff=$(cat "./.screenoff.tmp")
  else
      screenoff=$(gsettings get org.gnome.desktop.session idle-delay \
                   | awk '{print $2}')
      echo "$screenoff" > "./.screenoff.tmp"
  fi

# switch power saving off:
  gsettings set org.gnome.desktop.session idle-delay 0

  echo "lots of important information being printed..."

# Set "turn screen off" back to original value:
  gsettings set org.gnome.desktop.session idle-delay "$screenoff"

# remove temporary file
  rm "./.screenoff.tmp"

相关内容