Shell脚本 - [: - lt:预期的一元运算符

时间:2018-05-15 11:45:41

标签: shell unary-operator

#!/bin/bash
#ps -aux | grep abcd > /home/test1.txt
var= grep -o -i abcd /home/test1.txt | wc -l
threshold=15
if [ $var -lt $threshold ]; then
echo "One of the service is down on $HOSTNAME" >mail.txt
mailx -s "Application alert on $HOSTNAME" myname@domain.com <mail.txt
fi
if [ $var -eq $threshold ]; then
echo "All services are up and running fine on $HOSTNAME" >mail.txt
mailx -s "Application alert on $HOSTNAME" myname@domain.com <mail.txt
fi
exit;

我得到[:-lt:一元运算符期望和[:-eq:一致运算符在if循环启动时预期。任何人都可以帮忙,为什么会发生这种情况?

1 个答案:

答案 0 :(得分:-1)

在if语句中缩进代码块:

#!/bin/bash
var= grep -o -i abcd /home/test1.txt | wc -l
threshold=15
if [ "$var" -lt "$threshold" ]; then
    echo "One of the service is down on $HOSTNAME" >mail.txt
    mailx -s "Application alert on $HOSTNAME" myname@domain.com <mail.txt
fi
if [ "$var" -eq "$threshold" ]; then
    echo "All services are up and running fine on $HOSTNAME" >mail.txt
    mailx -s "Application alert on $HOSTNAME" myname@domain.com <mail.txt
fi
exit;

(或删除回声)