自定义命令 - 比当前亮度级别低一

自定义命令 - 比当前亮度级别低一

我尝试使用 xbacklight,但它不起作用,所以我制作了一些自定义全局命令来控制亮度。例如:

“明亮的” :

sudo bash -c "echo 20 > /sys/class/backlight/acpi_video0/brightness"

“中” :

sudo bash -c "echo 10 > /sys/class/backlight/acpi_video0/brightness"

“黑暗的” :

sudo bash -c "echo 0 > /sys/class/backlight/acpi_video0/brightness"

我想要再创建两个命令,一个将亮度增加一级,另一个将亮度降低一级。

我可以使用一些代码来代替“echo“NUMBER””,以将当前值减少/增加一。

我知道当前的亮度水平文档:/sys/class/backlight/acpi_video0/brightness 当前读取“20”,并在使用亮度命令时发生变化。

我不知道这个命令会是什么样子,但是像

sudo bash -c "echo [ONE LESS THAN] /sys/class/backlight/acpi_video0/brightness > /sys/class/backlight/acpi_video0/brightness" 

我不知道这是否可能,但还是感谢您的帮助。

更新

正确的用法是什么?

当我运行“sudo ./brightness.sh +1”时,它输出:

Usage:
sudo brightness.sh [ + | - | INTEGER ]
./brightness.sh: 10: [: +: unexpected operator
./brightness.sh: 23: [: +: unexpected operator
./brightness.sh: 38: ./brightness.sh: [[: not found
<<< ERROR: wrong parameter 
./brightness.sh: 44: ./brightness.sh: printUsage: not found

当我运行“sudo bash bright.sh +1”时,它输出:

Usage:
sudo brightness.sh [ + | - | INTEGER ]
brightness.sh: line 27: [: -1: unary operator expected
brightness.sh: line 34: : No such file or directory

答案1

为什么不制作一个根据您传递给它的选项增加一或减少一的命令呢?这就是您将在下面看到的脚本背后的基本思想。我们将当前值存储在CURRENT变量中,然后使用bash算术扩展$(( numberA + numberB ))来增加或减少一。至于决策,我们使用单个命令行参数调用脚本+来增加或-减少。

根据评论中 muru 的建议,我还对代码做了一点小修改,现在您可以brightness.sh使用小于或等于最大亮度的整数参数进行调用

#!/bin/bash

###########
# Variables
###########
FILE="/sys/class/backlight/acpi_video0/brightness"
MAX="$(cat /sys/class/backlight/acpi_video0/max_brightness)"
MIN=0
CURRENT="$( cat  $FILE )"

###########
# functions
###########

function printUsage
{
  echo "Usage:"
  echo "sudo brightness.sh [ + | - | INTEGER ]"
}

######
# Main
######


if [ $# -eq 0  ];
then
   printUsage
   exit 0
fi


if [ "$1" == "+" ];
then
   NEW=$(( $CURRENT+1  ))
   # are we trying to go past maximum value ? If yes, quit
   if [ $NEW -ge $MAX  ]
   then 
       echo ">>> ERROR: Current value already the maximum; "
       echo ">>> Exiting"
       exit 1
   fi
   # if not, then proceed
   echo "$NEW" > "$FILE"

elif [ "$1" == "-"  ];
then
   NEW=$(( $CURRENT-1  ))
   # Is NEW value going past 0 ? If yes, quit
   if [ $NEW -le $MIN ]; 
   then
       echo ">>> Error: already at minimum. "
       echo ">>> Exiting."
       exit 1
    fi
    # If not, then continue
    echo "$NEW" >  "$FILE"

# Note: new test w/ regex; for portability
# probably better idea to use awk
elif [[ "$1" =~ ^[0-9]+$ ]] && [ $1 -le $MAX   ];
then
    echo "$1" > "$FILE"

else
    echo "<<< ERROR: wrong parameter "
    printUsage
    exit 1
fi

相关内容