![[: shell 中缺少 `]' 错误,[] 中的代码前后已有空格](https://linux22.com/image/151951/%5B%3A%20shell%20%E4%B8%AD%E7%BC%BA%E5%B0%91%20%60%5D'%20%E9%94%99%E8%AF%AF%EF%BC%8C%5B%5D%20%E4%B8%AD%E7%9A%84%E4%BB%A3%E7%A0%81%E5%89%8D%E5%90%8E%E5%B7%B2%E6%9C%89%E7%A9%BA%E6%A0%BC.png)
代码:
if [ $mintemp -ge 15 && $maxtemp -le 28 ]; then echo "nice day"; fi
我在 [ 和 $Mintemp 、 28 和 ] 之间已经有空格
但错误仍然发生
答案1
&&
不是一个[]
参数(因为[
实际上是一个程序),它是 Bash 的运算符,用于在第一个命令成功时运行第二个命令。所以对于 bash 你的脚本看起来像:
if ([ $mintemp -ge 15) && ($maxtemp -le 28 ]; then echo "nice day"; fi)
这是无效的。但是你可以这样做:
if [ $mintemp -ge 15 ] && [ $maxtemp -le 28 ]; then echo "nice day"; fi