cat 和 if 的 Bash 脚本错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12636170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash script error with cat and if
提问by HumiD
I'm learning bash but now I'm having a lot of trouble making this script work:
我正在学习 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
The error that I get when I execute this script is:
执行此脚本时出现的错误是:
./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
I have been reading and looking around and I think the catoutput is correctly assigned to BATTERY. I tried with some different things like {[¨, but nothing works.
And yes, the file exists and it's in the same folder with the script.
我一直在阅读和环顾四周,我认为cat输出已正确分配给电池。我尝试了一些不同的东西,比如{[¨,但没有任何效果。是的,该文件存在并且与脚本位于同一文件夹中。
Any advice?
有什么建议吗?
回答by Mat
BATTERY='cat battery.txt'
That doesn't execute cat battery.txt, it just saves "cat battery.txt" as a string into that variable.
这不会执行cat battery.txt,它只是将“cat battery.txt”作为字符串保存到该变量中。
You should:
你应该:
BATTERY=$(cat battery.txt)
or
或者
BATTERY=`cat battery.txt`
(First form is preferred.)
(首选第一种形式。)
Your tests have syntax errors too. Use for example:
您的测试也有语法错误。使用例如:
elif [[ $BATTERY -le 10 && $BATTERY -gt 0 && $G -eq 0 ]]; then ...
[and [[are actually completely different things.
[并且[[实际上是完全不同的东西。
[is the testprogram (check out ls /usr/bin/[and man test), [[ expr ]]is a shell compound command (conditional expression).
[是test程序(签出ls /usr/bin/[和man test),[[ expr ]]是外壳复合命令(条件表达式)。
回答by choroba
The correct syntax for conditions is
条件的正确语法是
[[ $BATTERY -le 55 && $BATTERY -gt 40 && $D -eq 0 ]]
i.e. no single square brackets. For numeric comparison, you can use the double parentheses, too:
即没有单个方括号。对于数字比较,您也可以使用双括号:
if (( BATTERY==55 && BATTERY > 40 && D == 0 )) ; then ...
回答by Blender
To get the output of a command, you need to wrap the command with backticks, not quotes:
要获得命令的输出,您需要用反引号而不是引号将命令包装起来:
BATTERY=`cat battery.txt`
^ ^

