bran
bran

Reputation: 167

Save Bash script output to text file with conditions

I am using this script to check a list of ips I own to see if they are on the spam block list.

auto.sh:

while read ip ; do
    ./blacklist.sh $ip
done < block.txt

blacklist.sh is the above linked script. block.txt lists each of my ips one line at a time (I have several /22).

A typical output of a blocked ip scan looks like this:

Warning: PTR lookup failed
b.barracudacentral.org : 127.0.0.2
bb.barracudacentral.org : 127.0.0.2
black.junkemailfilter.com : 127.0.0.2
cbl.abuseat.org : 127.0.0.2
cidr.bl.mcafee.com : 127.0.0.4
dnsbl.justspam.org : 127.0.0.2
hostkarma.junkemailfilter.com : 127.0.0.2

----------------------------------------------------------
Results for <my ip>

Tested:        117
Passed:        110
Invalid:       0
Blacklisted:   7
----------------------------------------------------------

what I want to do is have the script spit out output to a file when the text above doesn't say "Blacklisted: 0".

I am not sure how to approach this, will this work?

sudo ./auto.sh "conditions where Blacklisted: is > 0" >> 12.txt

Thanks for any help

Upvotes: 0

Views: 105

Answers (1)

jofel
jofel

Reputation: 3405

Put the output in a temporary file and then check its content:

./auto.sh > 12_temp.txt
grep -q 'Blacklisted:[ \t]*0$' 12_temp.txt || cat 12_temp.txt >> 12.txt
rm -f 12_temp.txt

Upvotes: 1

Related Questions