yishai
yishai

Reputation: 27

IF statement does not perform the condition (bash)

this is the bash code:

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

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

the problem is that the email is sent even if the word "changed" not exist in the file2.txt what the problem with the condition ?

Upvotes: 0

Views: 134

Answers (2)

yishai
yishai

Reputation: 27

I solved it: the "send" variable that exist above in my script (I didn't post it in my QA) sent the email anyway, without even arriving to IF statement, I don't know why it is like that , but when I put the send command (sendmail) into the IF statement.. the email was not sent, of course I simulate situation that the word "changed" exist and the email was sent

many thanks to all the helpers

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

Upvotes: 1

Nic3500
Nic3500

Reputation: 8621

Try:

#!/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 ensures that only the word "changed", without any other character before or after it is considered. "itchanged" or "changed123" will not trigger the email.
  • grep -c will only return something. 0 if not there, some number > 0 if it is there.
  • You did not mention the value of $send, so I put something.

Upvotes: 0

Related Questions