bash启动键盘灯不运行

bash启动键盘灯不运行

我创建了一个 bash 脚本来允许键盘灯在特定时间打开。然而我还无法让它运行。我已经验证了“light”命令并且它确实有效。我还包含了 echo 语句来检查条件是否满足,但似乎它们被一起跳过了。

我确实知道脚本运行成功,因为我已将输出“测试”绘制到文件中并且该命令已写入...

有谁知道我做错了什么?

抱歉这个菜鸟问题......

#!/bin/bash
currenttime=$(date +%H:%M)
echo test
if [[ "$currenttime" > "17:00" ]]; then
    if [[ "$currenttime" < "18:00" ]]; then
        light -Srs "sysfs/leds/chromeos::kbd_backlight" 25
        echo 1
    fi
fi

if [[ "$currenttime" > "18:00" ]]; then
    if [[ "$currenttime" < "19:00" ]]; then
        light -Srs "sysfs/leds/chromeos::kbd_backlight" 50
        echo 2
    fi
fi

if [[ "$currenttime" > "19:00" ]]; then
    if  [[ "$currenttime" < "20:00" ]]; then
        light -Srs "sysfs/leds/chromeos::kbd_backlight" 75
        echo 3
    fi
fi

if [[ "$currenttime" > "20:00" ]]; then
    if [[ "$currenttime" < "06:00" ]]; then
        light -Srs "sysfs/leds/chromeos::kbd_backlight" 100
        echo 4
    fi
fi

if [[ "$currenttime" < "07:00" ]]; then
    if [[ "$currenttime" > "06:00" ]]; then
        light -Srs "sysfs/leds/chromeos::kbd_backlight" 50
        echo 5
    fi
fi

if [[ "$currenttime" > "7:00" ]]; then
    if [[ "$currenttime" < "17:00" ]]; then
        light -Srs "sysfs/leds/chromeos::kbd_backlight" 0
        echo 6
    fi
fi

答案1

您不是针对 $currentime 中的值进行测试,而是针对具有值 $currentime 的 str 进行测试。您需要删除“$currenttime”周围的引号

就像这样:

if [[ $currenttime > "17:00" ]]; then

但在 cronjob 中运行命令可能会更简单。我不知道你如何启动这个脚本,但如果你用 cronjob 来执行它并每分钟执行一次,那么每分钟轻程序都会收到一个调整值的命令。我只在您想要的确切设定时间执行命令会更有意义

0 18 * * * light -Srs "sysfs/leds/chromeos::kbd_backlight" 25 >/dev/null 2>&1

0 19 * * * light -Srs "sysfs/leds/chromeos::kbd_backlight" 50 >/dev/null 2>&1

0 20 * * * light -Srs "sysfs/leds/chromeos::kbd_backlight" 100 >/dev/null 2>&1

0 6 * * * light -Srs "sysfs/leds/chromeos::kbd_backlight" 50 >/dev/null 2>&1

0 7 * * * light -Srs "sysfs/leds/chromeos::kbd_backlight" 0 >/dev/null 2>&1

编辑:

或者每分钟运行一次脚本并将灯光值增加(max_light_val / minutes_till_next_setting).那会很有趣:)

相关内容