我学习bash的,但现在我有很多的麻烦使这个脚本工作:bash脚本错误,如果
#!/bin/bash
A="0"
B="0"
C="0"
D="0"
E="0"
F="0"
G="0"
while true; do
sleep 1
BATTERY='cat battery.txt'
if [["$BATTERY" -le 100] && ["$BATTERY" -gt 85] && [$A -eq 0]]; then
A="1"
commands...
elif [["$BATTERY" -le 85] && ["$BATTERY" -gt 70] && [$B -eq 0]]; then
B="1"
commands...
elif [["$BATTERY" -le 70] && ["$BATTERY" -gt 55] && [$C -eq 0]]; then
C="1"
commands...
elif [["$BATTERY" -le 55] && ["$BATTERY" -gt 40] && [$D -eq 0]]; then
D="1"
commands...
elif [["$BATTERY" -le 40] && ["$BATTERY" -gt 25] && [$E -eq 0]]; then
E="1"
commands...
elif [["$BATTERY" -le 25] && ["$BATTERY" -gt 10] && [$F -eq 0]]; then
F="1"
commands...
elif [["$BATTERY" -le 10] && ["$BATTERY" -gt 0] && [$G -eq 0]]; then
G="1"
commands...
fi
done
当我执行这个脚本,我得到的错误是:
./changewill.sh: line 17: [[cat battery.txt: command not found
./changewill.sh: line 27: [[cat battery.txt: command not found
./changewill.sh: line 36: [[cat battery.txt: command not found
./changewill.sh: line 45: [[cat battery.txt: command not found
./changewill.sh: line 54: [[cat battery.txt: command not found
./changewill.sh: line 63: [[cat battery.txt: command not found
./changewill.sh: line 72: [[cat battery.txt: command not found
我一直在阅读和四处张望,我认为cat
输出正确分配给BATTERY。我尝试了一些不同的东西,如{
[
¨
,但没有任何工作。 是的,文件存在,它与脚本位于同一个文件夹中。
有什么建议吗?
您正在脚本的目录中运行脚本还是将它放在路径中? – BugFinder
试试[this](http://stackoverflow.com/a/4139214/645270)。 – keyser
'[['和'[''需要用空格包围 - 并且参阅[bash手册](http://www.gnu.org/software/bash/manual/html_node/index.html) [''(http://www.gnu.org/software/bash/manual/html_node/Bourne-Shell-Builtins.html#Bourne-Shell-Builtins)(查找“test”)和[' [['](http://www.gnu.org/software/bash/manual/html_node/Conditional-Constructs.html#Conditional-Constructs)。 –