我正在尝试编写一个 bash 函数,以切换联想笔记本电脑中的电池保护功能,就像在 Windows 中一样。
但是,当我运行时,它给出了下面的命令未找到错误,并且它转到 else 部分。请帮我修复这个 if 条件。var 从 cat 中获取 0 或 1
[1:未找到命令
toggle_conservation () {
var=$(cat /sys/bus/platform/drivers/ideapad_acpi/VPC2004:00/conservation_mode)
if [$var -eq 1]
then
sudo bash -c 'echo 0 > /sys/bus/platform/drivers/ideapad_acpi/VPC2004:00/conservation_mode'
echo "Conservation is off, bettery will charge upto 100%"
else
sudo bash -c 'echo 1 > /sys/bus/platform/drivers/ideapad_acpi/VPC2004:00/conservation_mode'
echo "Conservation is enabled, battery wont charge beyond 60-70%"
fi
}
答案1
这是有效的,必须在 if 条件中添加一个条件并删除前面的空格。
toggle_conservation () {
my_value=$(cat /sys/bus/platform/drivers/ideapad_acpi/VPC2004:00/conservation_mode)
if [ $my_value -gt 0 ]
then
# If variable equal to 1
sudo bash -c 'echo 0 > /sys/bus/platform/drivers/ideapad_acpi/VPC2004:00/conservation_mode'
echo "Conservation is off, bettery will charge upto 100%"
else
sudo bash -c 'echo 1 > /sys/bus/platform/drivers/ideapad_acpi/VPC2004:00/conservation_mode'
echo "Conservation is enabled, battery wont charge beyond 60-70%"
fi
}