我在这个bash脚本中做错了什么?

问题描述 投票:0回答:2

我一直在摸索这个bash脚本已经有一段时间了但是我对bash脚本不太熟悉以找出问题所在。这是我的脚本:

    #!/usr/bin/bash

# Battery level warning script

NOTIFIED=0

while true; do

    BATTERY_LEVEL=$(cat /sys/class/power_supply/BAT0/capacity)
    STATE=$(cat /sys/class/power_supply/BAT0/status)

    echo "beep"
    # notify user of battery level
    if [ $STATE == "Discharging" ] ; then
        echo "Discharging"
        if [ $NOTIFIED -lt "1" && $BATTERY_LEVEL -lt "50" && $BATTERY_LEVEL -gt "10" ]; then
            echo "Battery >10%, < 50%"
            sudo -u korgan DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus notify-send "Battery low warning" "Battery level is ${BATTERY_LEVEL}%" --icon=battery-low
            NOTIFIED=1
        elif [ $NOTIFIED -lt "2" && $BATTERY_LEVEL -lt "11" && $BATTERY_LEVEL -gt "5" ]; then
            echo "Battery >5%, <11%"
            sudo -u korgan DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus notify-send "Battery very low warning" "Battery level is ${BATTERY_LEVEL}% Use ac power now, or shutdown is imminent - close applications"  --icon=battery-caution
            NOTIFIED=2
        elif [ $NOTIFIED -lt "3" && $BATTERY_LEVEL -lt "6" ]; then
            echo "Battery <6%"
            sudo -u korgan DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus notify-send "Battery critical warning" "Battery level is ${BATTERY_LEVEL}% Shutting system down now"
            NOTIFIED=3
        fi
    elif [ $STATE == "Charging" ]; then
            echo "Charging"
        NOTIFIED=0
    fi

    if [ $BATTERY_LEVEL -gt "60" ]; then
        echo "Battery > 60%: waiting 10 secs"
        sleep 10
    else
        echo "Battery <=60%: waiting 5 secs"
        sleep 5
    fi
done

我的目标是在电池放电和一定电量时收到通知发送警告。我得到的错误是

./battery_check.sh: line 16: [: missing `]'
./battery_check.sh: line 20: [: missing `]'
./battery_check.sh: line 24: [: missing `]'
Battery > 60%: waiting 10 secs
bash
2个回答
3
投票

更改:

if [ $NOTIFIED -lt "1" && $BATTERY_LEVEL -lt "50" && $BATTERY_LEVEL -gt "10" ]; then`

if [ "$NOTIFIED" -lt "1" ] && [ "$BATTERY_LEVEL" -lt "50" ] && [ "$BATTERY_LEVEL" -gt "10" ];  then  ....`

0
投票

您在第16行,第20行和第24行有语法错误。它应该是

if [ $NOTIFIED -lt "1" ] && [ $BATTERY_LEVEL -lt "50" ] && [ $BATTERY_LEVEL -gt "10" ];
© www.soinside.com 2019 - 2024. All rights reserved.