[CentOS] Bash Script for Beginners! oh dear :'(

John Doe jdmls at yahoo.com
Tue Dec 23 12:22:45 UTC 2008


> Scenario:
> /some/directory/where/blacklist/is/stored contains about 40-50 folders
> called, adult, gambling, banking, warez etc. There is a folder for
> each blocking category (and in each folder is two files, urls and
> domains, standard stuff for web filtering!)
> 
> I have a script to search through
> /some/directory/where/blacklist/is/stored and look at each text file
> trying to find someblockedsite.com and remove it. This is as far as I
> have got:
> 
> machine:/blacklistdir# sh ./find_files "blockedsite.com"
> 
> find_files is as follows:
> 
> #!/bin/bash
> rm -f ./found_files
> touch ./found_files
> find . -exec grep -q "$1" '{}' \; -print >> ./found_files
> i=1
> while [ $i -le `wc -l ./found_files` ] ; do
> 
>     grep -iv $1 $2 > $2.new   ####This is where I am stuck, I have put
> $2 but I want to be reading each line of text from found_files?
>     rm $2
>     mv $2.new $2
> 
> done

Something like this?

find . -exec grep -q "$1" '{}' \; -print | while read BLOCKFILE; do
    grep -iv "$1" $BLOCKFILE > $BLOCKFILE.new
    mv -f $BLOCKFILE.new $BLOCKFILE
done


      



More information about the CentOS mailing list