Do nothing if column1 is found

Hello,
I have stucked at one point.
When I run the script, I am asking the script to search in database file and if it's found, do nothing and proceed to next line in database file.

#!/bin/bash
./extract_email.pl output2 > database
while read -r COL1
    X=$(grep $COL1 database )
    if [[ -z $X ]] ; then
        echo "false"
        exit
else 
do
sed -n '1,2p' /var/test/test.txt > $COL1
sed -e '1,2d' /var/test/test.txt > /var/test/test2.txt
mail -s "mail subject is here" -a "From: info@mymail.net" $COL1 < $COL1
mv $COL1 /var/test/database_pool/
done < database
rm output*
rm *.host
exit 0

when i run the script, it says " syntax error near unexpected token `do' "

Could you please let me know how "do nothing" if col1 is already in my database.

Thanks in advance
Boris

First of all, let me ask you something:

Why are you trying to grep something that you read from same input file?

X=$(grep $COL1 database )

done < database
1 Like

You've got several syntactical and semantical errors, e.g.

  • while read; do ... done is not met
  • if ... fi is not met
  • mail ... $COL1 < $COL1 uses $COL as the URL and tries to read from it at the same time

But, in the first place, as Yoda pointed out, the entire if branch will never be executed as COL1 read from database will always be found in database ...

1 Like

Hello Yoda,
Thanks for your reply.
You are right but normally if there was no error with script, should it be working?

I have changed it as follows:

#!/bin/bash
./extract_email.pl output2 > database2
while read -r COL1
    X=$(grep $COL1 database )
    if [[ -z $X ]] ; then
        echo "false"
        exit
else 
do
sed -n '1,2p' /var/test/test.txt > $COL1
sed -e '1,2d' /var/test/test.txt > /var/test/test2.txt
mail -s "mail subject is here" -a "From: info@mymail.net" $COL1 < $COL1
mv $COL1 /var/test/database_pool/
done < database2
rm output*
rm *.host
exit 0

I already have database and database2 files in the folder

Thanks in advance
Boris

---------- Post updated at 02:22 PM ---------- Previous update was at 12:27 PM ----------

Thanks for your help Rudic and Yoda.

grep -x -f 

I have sorted out the problem without using if circle.

b.regards
Boris

Did you resolve all the concerns? Brilliant. Why don't you post your resulting script as a reference for others?

1 Like

I know this code is not good but works for me:

#!/bin/bash
cd /home/info/Maildir/new/
sed -n '/Return/p' *.host > output
sed 's/</ /' output > output1
sed 's/>/ /' output1 > output2
./extract_email.pl output2 > database
diff -U $(wc -l < database) database emails | grep '^-' | sed 's/^-//g' > database2
while read -r COL1
        do
mail -s "mail subject is here" -a "From: info@myemail.net" $COL1 < $COL1
done < database2
exit 0

thanks for your help
Boris

1 Like

You know you can do several replacements with one sed command:

sed -n '/Return/{ s/<|>/ /g; p}' *.host > output

(untested)? Same holds true for the grep ... | sed ... pipe. And still I don't understand the mail ... $COL1 < $COL1 construct...

1 Like

COL1 is not an important thing. I just want to know what and to whom I sent. For that reason, I keep sent mail record by the name of each recipient. Postfix is not configured exactly. Just spf record is okay.

Thanks again
Boris