![if 条件 .Bash 脚本中未处理变量](https://linux22.com/image/156014/if%20%E6%9D%A1%E4%BB%B6%20.Bash%20%E8%84%9A%E6%9C%AC%E4%B8%AD%E6%9C%AA%E5%A4%84%E7%90%86%E5%8F%98%E9%87%8F.png)
我有一个简单的 bash 脚本
#!/bin/bash
Result =$(zgrep -i "blocked session" /BACKUP/server.log.2019-06-23-06.gz | wc -l)
if [[ "$Result" -ge 1 ]];
then
echo "CRITICAL : The error string is found $Result times in server.log file"
exit 2
else
echo "OK: No instances of the error string in the server.log file "
exit 0
fi
wc -l 正确捕获了大约 1744 个错误字符串实例,但未在 if 条件中使用,并且我得到了错误的退出代码
++ zgrep -i "blocked session" /BACKUP/server.log.2019-06-23-06.gz
++ wc -l
+ Result =1744
/tmp/test.sh: line 4: Result: command not found
+ [[ '' -ge 1 ]]
+ echo OK: No instances of the error string in the server.log file '
OK: echo "OK: No instances of the error string in the server.log file
+ exit 0
请让我知道我在这里缺少什么。
答案1
删除第二行的空格:
Result=$(zgrep -i "blocked session" /BACKUP/server.log.2019-06-23-06.gz | wc -l)
代替
Result =$(zgrep -i "blocked session" /BACKUP/server.log.2019-06-23-06.gz | wc -l)