![调试错误:./vamsitest.sh:第 10 行:[:缺少 `]'](https://linux22.com/image/61712/%E8%B0%83%E8%AF%95%E9%94%99%E8%AF%AF%EF%BC%9A.%2Fvamsitest.sh%EF%BC%9A%E7%AC%AC%2010%20%E8%A1%8C%EF%BC%9A%5B%EF%BC%9A%E7%BC%BA%E5%B0%91%20%60%5D'.png)
脚本:它给出以下错误。基本上,当内存超过某个限制时,我需要收到电子邮件。
error : ./vamsitest.sh: line 10: [: missing `]'
#! /bin/bash
# Total memory space details
echo "Memory Space Details"
free -t -m | grep "Total" | awk '{ print "Total Memory space : "$2 " MB";
print "Used Memory Space : "$3" MB";
print "Free Memory : "$4" MB";
}'
if [ "$3" MB" >10000 MB];
then
email -s "memory utilization is high" [email protected]
fi
答案1
Shell 变量 $3 未定义。您似乎假设它是 awk 变量 $3。并且您在 shell 测试操作中使用了错误的语法(引用!)。
在 shell 中完成所有操作,或者在 awk 中完成所有操作。
在 awk 中...
echo "Memory Space Details"
free -t -m | awk '
/Total/ {
print "Total Memory space : "$2 " MB"
print "Used Memory Space : "$3" MB"
print "Free Memory : "$4" MB"
if ($3 > 10000)
system ("email -s ...")
}'
在 shell 中(例如 bash)...
echo "Memory Space Details"
set $( free -t -m | grep "Total")
printf "Total Memory space : %s MB\n" "$2"
printf "Used Memory Space : %s MB\n" "$3"
printf "Free Memory : %s MB\n" "$4"
(( $3 > 10000 )) && email -s "..."