IF语句不执行条件(bash)

IF statement does not perform the condition (bash)

这是 bash 代码:

FILE=`cat /home/_nonlocl/file2.txt | grep -o changed`

if [  "$FILE" = "changed"  ]; then
    echo "sending mail $send"
else
    echo "fingerprint has not changed"
fi

问题是即使 file2.txt 中不存在“已更改”一词,也会发送电子邮件 条件有什么问题?

尝试:

#!/bin/bash
#
send="SOMETHING"

if [[ $(grep -wc "changed" file2.txt) -gt 0 ]]
then
    echo "sending mail $send"
else
    echo "fingerprint has not changed"
fi
  • grep -w 确保只考虑单词“changed”,而不考虑其前后的任何其他字符。 “itchanged”或“changed123”不会触发电子邮件。
  • grep -c 只会 return 东西。如果不存在则为 0,如果存在则为 > 0。
  • 你没有提到$send的价值,所以我放点东西。

我解决了: 我的脚本中上面存在的“发送”变量(我没有 post 它在我的 QA 中)无论如何发送了电子邮件,甚至没有到达 IF 语句,我不知道为什么会这样,但是当我将发送命令 (sendmail) 放入 IF 语句时.. 电子邮件是 not 发送的,当然我模拟了单词“已更改”存在并且电子邮件已发送

非常感谢所有的帮手

FILE=`cat /home/_nonlocl/file2.txt | grep -o changed`

if [  "$FILE" = "changed"  ]; then
    `echo -e sendmail -s bla bla bla....` 
else
    echo "fingerprint has not changed"
fi